条件发送错误消息(JSON,SQL)

时间:2015-04-24 14:23:38

标签: javascript json

首先对不起我的英语,我会尽量保持清醒。

我已经创建了一个函数,以便根据此json中的sql查询值返回使用json创建的表。

有时,sql查询是错误的,我无法进入我的json。 这就是为什么我有一个名为test的变量,它是在$.getJSON

中创建的

使用if,我检查是否定义了测试。如果不是,我会向用户发送提醒:

if(typeof test == "undefined"){ $('#loading').hide(); alert("Erreur dans la requete, veuillez modifier vos filtres."); //Error ! return; }

我的问题是,即使我的查询是正确的,我仍然会收到我的错误消息。我不知道为什么,因为如果我用test打印console.log(test),我会在代码中得到1 ...

如果你想看一下,这是我的整个功能:

function get_values(){ //Fonction permettant la creation du tableau de resultats

  $('#loading').show();

  $("table#mon_tableau").removeClass("hidden");

  var myjson = "http://******/get_json_test.php?callback=?";
  $('.filter').each(function(i, obj){
     var input = $(obj).find("input")[0];
     myjson += '&'+input.name+'=' + input.value;
  });

  $('.display').each(function(i,obj){
     myjson += '&'+$(obj).attr('table')+'|'+$(obj).attr('titre')+'';
  });

  console.log(myjson);

  $.getJSON(myjson, function(data){
     var titre_colindex = 0;
     var test = 1;
        if(data.length != 0){
           newRow = document.getElementById('mon_tableau').getElementsByTagName('tbody')[0].insertRow(-1);
              $.each(data[0], function(index, valeur){ //Pour chaque colonne
                 newCell = newRow.insertCell(titre_colindex);
                 newCell.innerHTML = index;
                 titre_colindex+=1;
              });

              $.each(data, function(i,ti){
                 var newRow;
                 var newCell;
                    newRow = document.getElementById('mon_tableau').getElementsByTagName('tbody')[0].insertRow(-1);
                    //newCell = newRow.insertCell(0);
                    //newCell.innerHTML = ti.TI;

                 var i_colindex=0;
                 $.each(ti, function(index, valeur){
                    //if(index != "TI"){
                       newCell = newRow.insertCell(i_colindex);
                       newCell.innerHTML = valeur;
                       i_colindex+=1;
                    //}
                 });
              }); //End each
           console.log(erreur);
           $('#loading').hide();
           alert("Il y a " + data.length + " resultats");
        }
        else{
           $('#loading').hide();
           $("table#mon_tableau").addClass("hidden");
           alert("Il n'y a aucun resultat...");
        }
  console.log(test);
  }); //End getJSON

  if(typeof test == "undefined"){
     $('#loading').hide();
     alert("Erreur dans la requete, veuillez modifier vos filtres.");
     return;
  }
  console.log(test);
} //End getvalues

1 个答案:

答案 0 :(得分:2)

00111100011100010101 是异步调用。它将执行调用,然后立即执行if语句返回数据,这就是为什么它总是未定义的。相反,您希望在成功或失败例程中包含此if语句。

理想情况下,您甚至不需要测试变量,因为您可以利用$.getJSON调用,因为它返回一个承诺,override func viewDidLoad() { let tap1 = UITapGestureRecognizer(target: self, action: "dismissKeyboard") self. UITextField1!.addGestureRecognizer(tap1) tap1.delegate = self } func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } func dismissKeyboard() { self.view.endEditing(true) } (成功)和getJSON(错误)例程。您的代码将更改为以下内容:

.done