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

Asked

Viewed 876 times

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 might be? I searched here for topics related to the subject but nothing solved. The strange thing is that in some returns of the request this error does not appear.

Follows call AJAX:

$.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);

    }
})
  • 1

    My guess is that in a certain situation the return is not a JSON. If the status is 200 the server returned ok, but if it entered the error jQuery found some error in processing the return.

  • @Does Lucascosta know any way around this error? Pq is not always that it returns this log. So for certain data it brings a JSON

  • The best way is to standardize in the back-end @jvbarson, so that the return is always JSON. If there is no data in the query, returns a JSON for example: { msg: "Não há dados" }. If the back-end gives error, return status 500 with a json with the error message, and so on.

  • The callback of error ajax, in the case of jQuery, receives three parameters: ( jqXHR, textStatus, errorThrown ). You are just checking a parameter, to get more details try reading the other two parameters...

  • @Panther has some example using the other parameters?

  • @Lucascosta but in the back-end I always return in json with json_encode from php, so much so that the server status is 200. So q n understand

  • Can you see the response received by the browser. In Mozilla, it is in the Veloper tool (F12), going in Network, when clicking on the request made, on the right side is header, request, response. Because he’s status 200, probably in response will be what was returned. Maybe a poorly formatted JSON, or only one text. Do this test :P

  • @Lucascosta ran the tests and when the request does not fail, in response a JSON appears, but when that error, the tab response says there are no data available to be seen.

  • That’s right, man, jQuery goes wrong because he can’t translate anything to JSON. The focus is you isolate what is the return when the error, and there in php see what the json_encode you are trying to code. Maybe you are doing empty. Don’t use PHP, but I think Voce has to focus on this..

  • @jvbarsou try something like this: error: function(error, textStatus, errorThrown){ console.log( error, textStatus, errorThrown); }.

  • @Good Panther, I think I discovered the reason for the error, in these cases, when it returns the object, I could see in the console that there was some accentuation error, see: https://s24.postimg.org/7glocl01x/Capturar.png

  • Hmm, try to put this in ajax: contentType: "application/json; charset=utf-8".

  • @Lucascosta error persists, I updated my code in the question to see how this

  • @jvbarsou Try using the global function utf8_encode() PHP in fields that may contain accents. The source in which you search for the information (database for example) must be in some encoding other than your HTML page.

  • @Panther fixed my problem. I made some treatments within mysql when bringing the data because inside the array was more complicated! Grateful!

Show 10 more comments

1 answer

2

The first argument of the error function within $.ajax not the error message, but the Ajax object (in this case jqXHR), as documented: http://api.jquery.com/jquery.ajax/:

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

The error message in the case is last String errorThrown, then to capture the error you must 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 server HTTP response, since the status is 200 (ie code 200 is when the server responds flawlessly, usually), the fault must be on account of the fact that the $.ajax expects a JSON, as you defined:

dataType: 'JSON'

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

That is, the problem is on the server side, you have to fix the filtraDados.php, because the downloaded data is causing fault in the "parse" of jQuery’s internal JSON.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.