【问题标题】:AJAX Call not working in Phonegap but working normallyAJAX 呼叫在 Phonegap 中不起作用但工作正常
【发布时间】:2013-12-21 08:34:46
【问题描述】:

我正在使用开放的天气地图 api 网络服务进行 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",但没有任何效果。请帮助我,因为我目前被困在这个问题上。

【问题讨论】:

  • 我忘了补充一点,我没有得到任何响应,即如果我执行以下操作,我的手机会收到失败警报:.done(function(msg){ if(msg){ //做所有的字符串化和解析 } else{ alert("fail"); } });

标签: ajax jquery cordova


【解决方案1】:

您是否已将 config.xml 中的 url 列入白名单?

<access origin="http://api.openweathermap.org" />

阅读更多:http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

【讨论】:

  • 嗨...我是电话间隙的新手,我没有 config.xml,而是在我的 res/xml 文件夹中有 plugins.xml 和 cordova.xml。那么我应该把这个放在哪里?
  • 查看链接,如果您在本地构建项目,这包括每个平台的说明:docs.phonegap.com/en/3.0.0/…
  • 已经有&lt;access origin="*" /&gt; 所以我肯定不需要那个吗?
【解决方案2】:

供以后搜索

你的项目路径/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:*" />

可能还需要安装“cordova-plugin-whitelist”插件:

科尔多瓦

cordova plugin add cordova-plugin-whitelist
cordova prepare

电话夹

phonegap plugin add cordova-plugin-whitelist
phonegap prepare

还要确保在您的 file.html 中添加必要的 Content-Security-Policy:

https://github.com/apache/cordova-plugin-whitelist#content-security-policy

【讨论】:

    【解决方案3】:
    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() {
    
            })  
    

    【讨论】:

      【解决方案4】:

      是白名单问题https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/ 安装它并转到 config.xml 以允许

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多