Ajax works asynchronously, that is, the request is sent and the rest of the code continues to run.
To capture and display the return value of the request, it is necessary to add these functions within% success%.
function getCategoria(id){
$.ajax({
url: "../control/anuncio/index.php",
data:{
method: 'get_categoria',
id_categoria: id
},
method: "post",
dataType: "json",
success: function(retorno){
exibeMensagem(retorno.categoria);
}
});
}
function exibeMensagem(msg) {
alert(msg);
}
Another way is to add the callback
option. Ex:
$.ajax({
url: "../control/anuncio/index.php",
async: false,
data:{
method: 'get_categoria',
id_categoria: id
},
But there is a problem. Current browsers display the alert:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check link .
The reason? This will soon be removed and you will not be able to use it. According to the API specification itself ...
Synchronous requests are in the process of removing the web platform as it has detrimental effects to the end user experience.
Developers should pass false to the asynchronous argument when the current global object is a async:false,
. Users are strongly encouraged to warn about such use in development tools and may generate the exception Window
Of course they will not remove from one to another, however the ideal is to suit as soon as possible. So it is recommended that you create another function to handle the return of the request (as shown in the first example) .