Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在odoojavascript中从url获取json数据_Javascript_Odoo_Odoo 10_Odoo 11 - Fatal编程技术网

如何在odoojavascript中从url获取json数据

如何在odoojavascript中从url获取json数据,javascript,odoo,odoo-10,odoo-11,Javascript,Odoo,Odoo 10,Odoo 11,如何使用JavaScript从url获取JSON数据。我尝试了以下方法,但没有给出json数据 var GetLocation = function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert("Geolocation is not supported

如何使用JavaScript从url获取JSON数据。我尝试了以下方法,但没有给出json数据

var GetLocation = function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else { 
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position, http) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var app_id = '6784429f5eff661309aaa5280a143f97';
    var api = "http://api.openweathermap.org/data/2.5";
    var weather_api = api +"/weather?appid="+app_id+"&lat="+lat+"&lon="+lon+"&units=metric";
    // console.log(weather_api);
    var a = http.get(weather_api).then(function(response){
        position.jsonList=response.data;

    });
    console.log(a);


}
当打印weather_api时,它给出了完整的url,但我一直在研究如何从该url获取json数据。

有几件事:

  • 您必须了解异步调用流
  • 如果你正在学习一个教程,你必须理解教程中的所有内容
  • #1
    请参阅异步调用流。然后学习如何使用JavaScript

    #2
    下面的教程将使用Angular.js,它内置了
    $http
    模块,其中包含一个
    get
    方法。在您的情况下,看起来您没有使用Angular.js&而且,
    showPosition
    函数被
    navigator.geolocation.getCurrentPosition
    调用,因此在调用
    showPosition
    函数时,它不会通过
    http
    模块

    我使用
    jQuery
    库添加了一个简单的http模块。因此,要使此代码正常工作,您必须在html文件中包含
    jQuery
    。在任何情况下,下面的代码都必须让您了解如何在JavaScript中创建对象和方法&如果您需要其他用于HTTP请求的库,您必须能够替换jQuery

    var http = {
      get: function(url) {
        return new Promise(function(resolve, reject) {
          $.get(url, resolve).fail(reject);
        })
      }
    };
    
    var GetLocation = function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } 
        else { 
            alert("Geolocation is not supported by this browser.");
        }
    }
    
    function showPosition(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        var app_id = '6784429f5eff661309aaa5280a143f97';
        var api = "http://api.openweathermap.org/data/2.5";
        var weather_api = api +"/weather?appid="+app_id+"&lat="+lat+"&lon="+lon+"&units=metric";
        // console.log(weather_api);
        var a = http.get(weather_api).then(function(response){
            position.jsonList=response;
            console.log(position.jsonList);
        });
    
    
    
    }
    

    注意:请注意,我已将
    console.log
    移动到promise处理程序中。

    将控制台日志放在回调函数中。您正在进行呼叫,记录未完成的承诺,然后呼叫正在完成。可能是@John212的重复,这与他们的问题无关。什么是
    http
    ?它用于获取数据,因为我从这里引用