2016-01-07 201 views
0

第一,请。原谅我格式化错误和缺乏知识。我是一个总jQuery和ajax noob。jquery ajax请求 - 空请求div输出

我通过ajax查询基于跨域jquery插件从james padolsey检索数据。接收到的数据被放入一个量程ID中。当我收到空的数据时,我想在同一范围内显示“无数据收到消息”,而不是空的结果。

我的代码看起来是这样的:

<span id="data"></span> 

<script> 

    $.ajax({ 

     url: "http://www.theurliloadfrom.com", 
     type: 'GET', 

     success: function(res) { 
      var theData = $(res.responseText).find('#extdata').text(); 
      $("#data").html(theData); 
     }, 

     error: function(){ 
      $("#data").html("There was an error"); 
     } 

    }); 

</script> 

有人可以帮助?

非常感谢!

回答

0

您可以通过以下条件

<script> 



$.ajax({ 

    url: "http://www.theurliloadfrom.com", 
    type: 'GET', 

    success: function(res) { 
      if(res != undefined) 
      { 
      var theData = $(res.responseText).find('#extdata').text(); 
      $("#data").html(theData); 
     } 
     else 
     { 
     $("#data").html("no data received message"); 
     } 
     }, 

    error: function(){ 
     $("#data").html("There was an error"); 
    } 

}); 

</script> 
+0

非常感谢你检查!我现在明白如果请求传递未定义,如何检查条件。它对我来说依然不起作用。 –