Mock ajax request
Par MD3804-GANDI 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!