Mock ajax request

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!

La discussion continue ailleurs

URL de rétrolien : https://davidmasclet.gisgraphy.com/index.php?trackback/46

Fil des commentaires de ce billet