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);
}
})
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.
– BrTkCa
@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
– João Vitor
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.– BrTkCa
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...– rdleal
@Panther has some example using the other parameters?
– João Vitor
@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
– João Vitor
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 isheader
,request
,response
. Because he’s status 200, probably inresponse
will be what was returned. Maybe a poorly formatted JSON, or only one text. Do this test :P– BrTkCa
@Lucascosta ran the tests and when the request does not fail, in
response
a JSON appears, but when that error, the tabresponse
says there are no data available to be seen.– João Vitor
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..– BrTkCa
@jvbarsou try something like this:
error: function(error, textStatus, errorThrown){ console.log( error, textStatus, errorThrown); }
.– rdleal
@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
– João Vitor
Hmm, try to put this in ajax:
contentType: "application/json; charset=utf-8"
.– BrTkCa
@Lucascosta error persists, I updated my code in the question to see how this
– João Vitor
@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.– rdleal
@Panther fixed my problem. I made some treatments within mysql when bringing the data because inside the array was more complicated! Grateful!
– João Vitor