2012-03-29 137 views
2

我正在使用以下代碼從我的數據庫中提取數據並用google地圖繪製點。我想要做的就是,「如果response = null,alert('empty')」,但每次我嘗試在代碼中使用它時,都會發生一些事情。如果任何人都可以提供任何可能的幫助。 這是我的代碼:Jquery ajax檢查結果是否爲空

<script type="text/javascript"> 
$(function() 

{ 
    var radius3 = localStorage.getItem("radius2"); 
    var lat3 = localStorage.getItem("lat2"); 
    var long3 = localStorage.getItem("long2"); 
    var type2 = localStorage.getItem("type2"); 
    var citya = localStorage.getItem("city2"); 
    var rep2 = localStorage.getItem("rep2"); 
    var size2 = localStorage.getItem("size2"); 
    var status2 = localStorage.getItem("status2"); 


    $.ajax({ 
     url: 'http://examplecom/test/www/base_search.php', 
     data: "city2=" + city2 + "&rep2=" + rep2 + "&status2=" + status2 + "&size2=" + size2 + "&type2=" + type2 + "&long2=" + long2 + "&lat2=" + lat2 + "&radius2=" + radius2, 
     type: 'post', 
     dataType: 'json', 
     success: function (data) { 
      if (data) { 
       $.each(data, function (key, val) { 


        var lng = val['lng']; 
        var lat = val['lat']; 
        var id = val['id']; 
        var name = val['name']; 
        var address = val['address']; 
        var category = val['category']; 
        var city = val['city']; 
        var state = val['state']; 
        var rep = val['rep']; 
        var status = val['status']; 
        var size = val['size']; 
        $('div#google-map').gmap('addMarker', { 
         'position': new google.maps.LatLng(lat, lng), 
         'bounds': true, 
         'icon': 'images/hospital.png' 
        }).click(function() { 
         $('div#google-map').gmap('openInfoWindow', { 
          'backgroundColor': "rgb(32,32,32)", 
          'content': "<table><tr><td>Name:</td><td>" + name + "</td></tr><tr><td>Address:</td><td>" + address + ", " + city + "&nbsp;" + state + "</td></tr><tr><td>Category:</td><td>" + category + "</td></tr><tr><td>Rep:</td><td>" + rep + "</td></tr><tr><td>Status:</td><td>" + status + "</td></tr><tr><td>Size:</td><td>" + size + "</td></tr></table>" 
         }, this); 


        }); 
       } else { 
        alert('hello'); 
       } 
       } 
      }) 

    } 
    }); 


}) 
} 



</script> 
+0

你在哪裏放'if(data)'? – gpasci 2012-03-29 15:54:43

+0

對不起,我只是更新了上面的代碼,我發佈了修改後的代碼 – 2012-03-29 16:00:37

+0

它工作嗎?在腳本的末尾有一些嵌套錯誤 – gpasci 2012-03-29 16:04:55

回答

11

喜歡的東西

success: function (data) { 
    if(!data) { 
     alert('empty'); 
     return; 
    } 
    $.each(data, function (key, val) { ... 

應該工作。

+0

謝謝你這麼多工作! – 2012-03-29 16:03:17

+2

@Nic - 我想知道如果我的回答沒有爲你工作,這是如何工作的。 – ShankarSangoli 2012-03-29 16:05:34

+1

@ShankarSangoli我真的不知道,我幾乎刪除了我的答案,當我看到你基本上發佈了完全相同的東西!但很高興它幫助:-) – Ben 2012-03-29 16:19:48

3

是這樣的?

 success: function (data) { 
      if(data){ 
       //do your stuff here 
      } 
      else{ 
       alert('empty'); 
      } 

     } 
+0

是的,我認爲這將工作讓我試試吧 – 2012-03-29 15:55:16

+0

所以我試了這個,它似乎沒有工作,我張貼我修改的代碼上面 – 2012-03-29 15:58:53

3

就是這樣的!

success: function (data) { 
if(data.length == 0) { 
    alert('empty'); 
    return; 
}