Mock ajax request
Par Masclet le vendredi 13 mars 2009, 17:47 - Divers - Lien permanent
Hi,
Here is a way to mock /stub an ajax http request with prototype :
Hi,
Here is a way to mock /stub an ajax http request with prototype :
<html>
<head>
<script src="/scripts/prototype.js" type="text/javascript"></script>
</head>
<body>
<h1>MocK jAjax</h1>
<script>
//we overide the request method to stub the response
Ajax.Request.addMethods( {
request: function(){
var mockTransport = new Object();
mockTransport.responseText= "mockit";
mockTransport.status=200;
mockTransport.onreadystatechange = Prototype.emptyFunction;
mockTransport.getResponseHeader= function(pname){
}
this.options.onSuccess(mockTransport);
return;
}
}
);
//a simple ajax request that alert the received text
doAjax = function() {
new Ajax.Request("jsonfile.txt", {
method: 'get',
evalJSON : false,
onSuccess: function(transport) {
alert(transport.responseText);
},
onFailure : function(transport){
alert("an error has occured");
},
encoding : "UTF-8",
});
}
doAjax();
</script>
</body>
</html>
Hope it helps!
Commentaires
Say "thanks" you to your parents that they gave you the world
I’m a long time watcher and I just believed I’d drop by and say hello there for your very first time.
I’m a long time watcher and I just believed I’d drop by and say hello there for your very first time.
I'll complain that you have copied material from another source
I like to read this type of stuff. Good and attractive information I take from it..Thank you for posting such a nice article.
I have read not one article on your blog. You're a big lad
I have not found what I wanted
Reading this article - the gift of your time
i can agree with the article
My spouse and I stumbled over here different website and thought I should check things out.