AJAX调用在Phonegap中不工作,但工作正常
我正在使用open weather map api webservice进行ajax调用,以便使用纬度和经度获取当前天气。问题是,相同的调用在我的普通php文件夹中有效,但在我的phongap应用程序中无效。我的ajax调用如下所示AJAX调用在Phonegap中不工作,但工作正常,ajax,jquery,cordova,Ajax,Jquery,Cordova,我正在使用open weather map api webservice进行ajax调用,以便使用纬度和经度获取当前天气。问题是,相同的调用在我的普通php文件夹中有效,但在我的phongap应用程序中无效。我的ajax调用如下所示 $.ajax({ type : "GET", dataType: "jsonp", url : "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=1
$.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);
});
});
我尝试过不使用数据类型:“jsonp”
尝试将其更改为“json”
,但没有任何效果。请帮助我,因为我目前正陷于此。var weather=“”
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 += '<div><strong>coord<strong><div>';
$.each(value, function(key, value) {
if(key == "lon")
weather += '<div>lon: '+value+'<div>';
if(key == "lat")
weather += '<div>lat: '+value+'<div>';
});
}
if(key == "weather"){
weather += '<div><strong>weather<strong><div>';
$.each(value, function(key, value) {
if(value.id)
weather += '<div>id: '+value.id+'<div>';
if(value.main)
weather += '<div>main: '+value.main+'<div>';
if(value.description)
weather += '<div>description: '+value.description+'<div>';
});
}
if(key == "main"){
weather += '<div><strong>main<strong><div>';
$.each(value, function(key, value) {
if(key == "temp")
weather += '<div>temp: '+value+'<div>';
if(key == "temp_min")
weather += '<div>temp_min: '+value+'<div>';
if(key == "temp_max")
weather += '<div>temp_max: '+value+'<div>';
if(key == "pressure")
weather += '<div>pressure: '+value+'<div>';
if(key == "sea_level")
weather += '<div>sea_level: '+value+'<div>';
if(key == "grnd_level")
weather += '<div>grnd_level: '+value+'<div>';
if(key == "humidity")
weather += '<div>humidity: '+value+'<div>';
});
}
});
alert(weather)
console.log(weather)
}
}).done(function() {
})
var ajax_call=”http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139";
$.ajax({
键入:“获取”,
url:ajax_call,
数据类型:“jsonp”,
成功:功能(响应){
$。每个(响应、功能(键、值){
//警报(键+“==”+值)
如果(键==“坐标”){
天气+='协调';
$.each(值、函数(键、值){
如果(键==“lon”)
天气+='lon:'+值+';
如果(键==“lat”)
天气+='纬度:'+数值+'';
});
}
如果(键==“天气”){
天气+='天气';
$.each(值、函数(键、值){
if(value.id)
天气+='id:'+value.id+'';
if(value.main)
天气+='main:'+value.main+'';
if(值说明)
天气+='说明:'+数值.说明+'';
});
}
如果(键==“主”){
天气+='main';
$.each(值、函数(键、值){
如果(键==“临时”)
天气+='温度:'+数值+';
如果(键==“最低温度”)
天气+='温度最低:'+数值+';
如果(键==“最高温度”)
天气+=“温度最大值:”+数值+”;
如果(键=“压力”)
天气+='压力:'+数值+'';
如果(键==“海平面”)
天气+='海平面:'+数值+'';
如果(键==“grnd_级别”)
天气+='地面高度:'+数值+';
如果(键==“湿度”)
天气+='湿度:'+数值+'';
});
}
});
警报(天气)
控制台日志(天气)
}
}).done(函数(){
})
您是否在config.xml中白名单上了url
<access origin="http://api.openweathermap.org" />
用于将来的搜索
yourprojectpath/config.xml
添加或检查以下行
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
音差
phonegap plugin add cordova-plugin-whitelist
phonegap prepare
还要确保在file.html中添加必要的内容安全策略:
这是一个白名单的问题
安装它并转到config.xml以允许我忘了添加我没有收到任何响应,即如果我执行以下操作,我会在手机中收到失败警报:。完成(函数(msg){if(msg){//do all stringify and parsing}否则{alert(“fail”);};您好……我是phone gap的新手,我没有config.xml,我的res/xml文件夹中有plugins.xml和cordova.xml。那么我应该把它放在哪里呢?请查看链接,如果您在本地构建项目,其中包括每个平台的说明:已经有
,所以我肯定不需要它?
phonegap plugin add cordova-plugin-whitelist
phonegap prepare