Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax XML Http Request not works

Tags:

jquery

ajax

There is an undefined error due to Ajax request in jQuery. But it works locally. Error referencing in jquery1.3.2.js @ 3633 line

xhr.send(s.data);

My code is:

$.ajax({
    type: "POST",
    url: 'index.php',
    data: "action=showpath&type=images&path=&default=1",
    cache: false,
    dataType: "html",
    success: function (data) {
        $('#addr').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

alerts in code shows me (0, 'undefined');

What I am doing wrong?

like image 201
Andrew Rumm Avatar asked Jun 21 '09 13:06

Andrew Rumm


People also ask

What does the XML http request object accomplish in Ajax?

The XMLHttpRequest object can be used to exchange data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

What is jqXHR in Ajax?

The jqXHR Object. The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.

What is the callback method present in XML http request Ajax?

onreadystatechange: It also defines a callback function that will be invoked when the readyState property changes. readyState: The readyState property defines the current state of the request or holds the current status of the XMLHttpRequest.


1 Answers

This could be happening if your ajax request is getting canceled before it completes. jQuery throws the error event when the user navigates away from the page either by refreshing, clicking a link, or changing the URL in the browser. You can detect these types of errors by by implementing an error handler for the ajax call, and inspecting the xmlHttpRequest object:

$.ajax({
    /* ajax options omitted */
    error: function (xmlHttpRequest, textStatus, errorThrown) {
         if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
              return;  // it's not really an error
         else
              // Do normal error handling
});
like image 99
curthipster Avatar answered Oct 18 '22 02:10

curthipster