ajax检查结果是否为空

ajax检查结果是否为空

我正在使用以下代码从数据库中提取数据并使用Google Maps绘制点。我想做的是“if response = null,alert('empty')”,但是每次我尝试将其用于这段代码时,都会中断。如果有人可以提供任何帮助,那就太好了。
这是我的代码:

<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>

最佳答案

就像是

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

应该管用。

关于javascript - jQuery ajax检查结果是否为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9928961/

10-10 11:37