AJAX Call不在Phonegap中工作,但工作正常
我正在使用开放天气地图api webservice进行ajax调用,以便使用纬度和经度来获取当前天气问题是在我的普通php文件夹中同样的调用,但它在我的phongap应用程序中不起作用。 我的ajax调用如下所示
$.ajax({ type : "GET", dataType: "jsonp", url : "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139", }).done( function(msg){ var response = JSON.stringify(msg); var parsedResponse = JSON.parse(response); alert(parsedResponse.main.temp_min); }); });
我试过没有dataType: "jsonp"
尝试将其更改为"json"
但没有任何作用。 请帮助我,因为我现在卡在这上面。
您是否已将config.xml中的URL列入白名单?
阅读更多: http : //docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide
var weather = "" var ajax_call = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"; $.ajax({ type: "GET", url: ajax_call, dataType: "jsonp", success: function(response){ $.each(response, function(key, value) { //alert(key+"====="+value) if(key == "coord"){ weather += 'coord'; $.each(value, function(key, value) { if(key == "lon") weather += 'lon: '+value+''; if(key == "lat") weather += 'lat: '+value+''; }); } if(key == "weather"){ weather += 'weather'; $.each(value, function(key, value) { if(value.id) weather += 'id: '+value.id+''; if(value.main) weather += 'main: '+value.main+''; if(value.description) weather += 'description: '+value.description+''; }); } if(key == "main"){ weather += 'main'; $.each(value, function(key, value) { if(key == "temp") weather += 'temp: '+value+''; if(key == "temp_min") weather += 'temp_min: '+value+''; if(key == "temp_max") weather += 'temp_max: '+value+''; if(key == "pressure") weather += 'pressure: '+value+''; if(key == "sea_level") weather += 'sea_level: '+value+''; if(key == "grnd_level") weather += 'grnd_level: '+value+''; if(key == "humidity") weather += 'humidity: '+value+''; }); } }); alert(weather) console.log(weather) } }).done(function() { })
供将来搜索
yourprojectpath / config.xml中
添加或检查以下行
它可能还需要安装“cordova-plugin-whitelist”插件:
cordova
cordova plugin add cordova-plugin-whitelist cordova prepare
PhoneGap的
phonegap plugin add cordova-plugin-whitelist phonegap prepare
另外,请确保在file.html中添加必要的Content-Security-Policy:
https://github.com/apache/cordova-plugin-whitelist#content-security-policy