我试图从此URL获取asciiName:
http://services.gisgraphy.com/geoloc/search?lat=21.283300399780273&lng=72.9832992553711&radius=10000<br>
使用ajax jsonp。
这是完整的代码:-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<Script>
var asciilast;
var asciiname="";
var lat="21.283300399780273";
var lng="72.9832992553711";
var radius="10000";
function getascii(){
$.ajax({
url: "http://services.gisgraphy.com/geoloc/search?lat="+lat+"&lng="+lng+"&radius="+radius+"&format=json",
async: false,
dataType:'jsonp',
success: function(data1) {
for(i=0;i<data1.result.length;i++) {
asciiname = asciiname+data1.result[i].asciiName+",";
console.log(asciiname);
asciilast = asciiname.replace(/\,$/, '');
}
alert(asciilast);
}
});
}
</Script>
<body onload="getascii()">
</body>
借助此代码,我可以获得完美的输出。
但有时会产生问题,例如某些结果没有asciiName,然后返回未定义。
如果结果没有asciiName,那么我不需要undefined
最佳答案
asciiname = '';
asciilast = '';
if(data1.result[i].asciiName){
asciiname = asciiname+data1.result[i].asciiName+",";
console.log(asciiname);
asciilast = asciiname.replace(/\,$/, '');
}