Monday 3 September 2018

error and full reminders are not called in firefox using jquery ajax

The following works as expected.

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
            else if (xmlhttp.status == 400) {
                alert('There was an error 400')
            }
            else {
                alert('something else other than 200 was returned:' + xmlhttp.status);
            }
        }
    }
    xmlhttp.open("GET", "/services", true);
    xmlhttp.send();

It alerts 'something else other than 200 was returned: 404'.
But the following ajax with jquery is not working.
     $.ajax({
        type: "get",
        url: "/services",
        error: function() {
            console.log("A");
        },
        complete: function (xhr, data) {
            console.log(arguments);
            if (xhr.status != 0)
                alert('success1');
            else
                alert('fail2');
        }
    })

Firebug shows the 404 but alert or console from above code is not called. I have been relying on jquery to not bother with cross browser issues, and now I am considering falling back to XMLHttpRequest().

It turned out that the issue was due to my build script mangling my code (including jquery). I dont know why it dont work after.
but setting optimize: "none",in the require build grunt file, made everything work.

0 comments:

Post a Comment