我使用Mapquest服务检查是否Ajax请求是空
我要检查,如果该请求是sucessfull的免费地理编码API,但不会返回任何数据。
我在表单域中输入“vdfsbvdf54vdfd”(只是一个愚蠢的字符串)作为地址。 我希望有一个类似于“抱歉,错误输入”的提醒。戒备从未发生。
这是我的代码片断
$.ajax({
type: "POST",
url: "`http://open.mapquestapi.com/geocoding/v1/address?key=012`",
data: { location: ad, maxResults: 1}
})
.done(function(response) {alert(response);
var geoclng = response.results[0].locations[0].latLng.lng;
var geoclat = response.results[0].locations[0].latLng.lat;
if (geoclng=="") {alert("Sorry, wrong input");}
//now use lon/lat on map etc
)}
我试图if (geoclng=="") {alert("Sorry, wrong input");}
也if (response.length=0) {alert("Sorry, wrong input");}
也if ($.isEmptyObject(response)) {alert("Sorry, wrong input");}
和警觉从未发生过。
如果有帮助,当我提醒我回应时,我得到object Object
。
在此先感谢
非常感谢细节,顺便说一句,URL上额外的单引号是将其标记为代码。不管怎样,谢谢 – slevin