error: Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}

0

I have an AJAX request that returns certain data from my database when the user filters some information, sometimes the filters work 100%, but sometimes it returns within the error of the request the following message:

Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}

Does anyone know what it can be? I searched the forum for topics related to the subject but nothing solved. The strange thing is that in some requests returns this error does not appear.

AJAX call follows:

$.ajax({
    url: 'datacenter/functions/filtraDados.php',
    type: 'POST',
    dataType: 'JSON',
    contentType: "application/json; charset=utf-8",
    data: {usuario: $("#dropdown-user").val()},
    success: function(data){
        $("#filtro-rede").text(data[0][0]['rede']);
        $("#filtro-loja").text(data[0][0]['loja']);
    },
    error: function(error){
        //alert('Ocorreu durante a execução do filtro. Tente novamente mais tarde!');
        console.log(error);

    }
})
    
asked by anonymous 23.01.2017 / 18:17

1 answer

0

The first argument of the error function within $.ajax is not the error message, but the Ajax object (in the case jqXHR ), as per the documentation: link :

  

Type: Function (jqXHR jqXHR, String textStatus, String errorThrown)

The error message in the case is last String errorThrown , so to catch the error you should use:

error: function(jqXHR, status, error){
    //alert('Ocorreu durante a execução do filtro. Tente novamente mais tarde!');
    console.log(status, error);

}

Because the error is occurring

The error occurs not because of communication failure or because some fault of some HTTP response from the server, since the status is 200 (ie code 200 is when the server responds faultlessly, usually >), the failure should be because $.ajax expects a JSON, as you defined:

dataType: 'JSON'

But your script datacenter/functions/filtraDados.php is returning or another response format, or has some failure in JSON, such as using an unsupported encoding (json uses UTF-8, otherwise it may fail), or this with some syntax error, or simply returning empty.

That is, the problem is on the server side, you have to fix the filtraDados.php , since the downloaded data is causing the jQuery internal JSON parse to fail.

    
18.04.2018 / 17:08