How to test XMLHttpRequest with Jasmine

后端 未结 6 1121
心在旅途
心在旅途 2021-02-19 16:43

How can I test the onreadystatechange on XMLHttpRequest or pure Javascript AJAX without jQuery? I\'m doing this because I\'m developing Firefox extension. I guess I have to use

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 17:20

    As per the facsimile of your code below you will likely be able to inject console.log() with the status code and status text.

    This will help you identify any http errors. Speculatively I'd say the status will return 404.

    submit : function() {
        var url = window.arguments[0];
        var request = new XMLHttpRequest();
        request.open("POST", 'http://'+this.host+'/doSomething', true);
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send("param="+param+"&emotions="+this.getParams());
        request.onreadystatechange = function() {
            console.log(this.status+ " - "+ this.statusText);
        };
    
    }
    

    An alternative to this would be to look at the request header / response within the firebug console. Which is a good point: If you're not using firebug or chrome dev tools you should change the console.log() call to something that appends the string to a document object do not use an alert() call.

    How do I verify jQuery AJAX events with Jasmine?. A similar answer that implements Jasmine.

提交回复
热议问题