AJAX Call not working in Phonegap but working normally

前端 未结 4 2097
时光取名叫无心
时光取名叫无心 2021-01-14 10:21

i am using the open weather map api webservice to make an ajax call inorder to get the current weather using the latitude and longitude the problem is the same call works in

相关标签:
4条回答
  • 2021-01-14 10:51

    For future searches

    yourprojectpath/config.xml

    Add or check the following lines

    <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:*" />
    

    It may also be required to install "cordova-plugin-whitelist" plugin:

    Cordova

    cordova plugin add cordova-plugin-whitelist
    cordova prepare
    

    Phonegap

    phonegap plugin add cordova-plugin-whitelist
    phonegap prepare
    

    Also make sure to add the necessary Content-Security-Policy in your file.html:

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

    0 讨论(0)
  • 2021-01-14 10:52

    it's a problem of white-list https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/ Install it and go to config.xml to allow

    0 讨论(0)
  • 2021-01-14 11:01

    Have you whitelisted the url in your config.xml?

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

    Read more: http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

    0 讨论(0)
  • 2021-01-14 11:04
    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() {
    
            })  
    
    0 讨论(0)
提交回复
热议问题