AJAX Call不能在Phonegap中工作但工作正常

时间:2013-12-21 08:34:46

标签: ajax jquery cordova

我正在使用开放天气地图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",但根本没有任何作用。请帮助我,因为我目前仍然坚持这一点。

4 个答案:

答案 0 :(得分:1)

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

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

了解详情:http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

答案 1 :(得分:0)

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() {

        })  

答案 2 :(得分:0)

用于将来的搜索

  

yourprojectpath / config.xml

添加或检查以下几行

<com.is.cut.App
    ...
    android:layout_above="@+id/adView"
    />

example screenshot

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

科尔多瓦

<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

cordova plugin add cordova-plugin-whitelist
cordova prepare

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

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

enter image description here

答案 3 :(得分:0)

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

相关问题