Check return $.getJSON

Asked

Viewed 783 times

1

I need to know if you returned anything from the database. Any suggestions?

var url = "agenda_salao_pesquisa.php?dataReserva="+busca;

$.getJSON(url, function(result) {
 //preciso saber se veio algo da consulta
            $.each(result, function(i, field) {

                var id = field.id;
                var dataReserva = field.dataReserva;
                var horaReserva =  field.horaReserva;
                var unidade = field.unidade;
                var responsavel = field.responsavel;
                var salao = field.salao;
   });
});
  • @Valdeirpsr nothing happens. Actually I need to fill a div if nothing returns.

  • the request brings some value or returns an empty value?

2 answers

1


The function of the method $.getJSON will only be executed if you see a valid JSON object. To verify if you have an error, empty stem, or invalid JSON, you need to add the callback .fail method (starting with jQuery version 3):

Version 3.0 or greater:

$.getJSON(url, function(result) {
   console.log("Deu certo. JSON válido!"); // aqui é a função success
})
.fail(function() {
   console.log("Deu erro. Veio nada, veio JSON inválido etc");
});

Version previous to 3.0:

$.getJSON(url, function(result) {
   console.log("Deu certo. JSON válido!"); // aqui é a função success
})
.error(function() {
   console.log("Deu erro. Veio nada, veio JSON inválido etc");
});

Full information you can refer to official method page.

0

You can use the if(result){}

He will return true if the return **: **

  • null
  • Undefined
  • Nan
  • Empty string ("")
  • 0
  • false

Source

  • I need to know if you did not return anything, I did if(!result){} and I did as Oce spoke but with I and nothing

  • you’ve already given a console.log(result) to see what’s coming?

  • nothing comes when the result is empty, when I put a parameter that returns something comes right

Browser other questions tagged

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