Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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
Javascript ajax未加载/检索数据_Javascript_Ajax - Fatal编程技术网

Javascript ajax未加载/检索数据

Javascript ajax未加载/检索数据,javascript,ajax,Javascript,Ajax,我正在做FreeCodeCamp课程,我正在尝试构建一个天气应用程序。我发现了一个关于如何通过地理定位获得纬度和经度的很好的教程。但现在,当我尝试运行应用程序时,它似乎没有检索ajax数据供我解析。我在本地尝试并将其移动到主机,我想这可能就是它,但现在我在html的第一行遇到了一个奇怪的错误,我没有看到任何错误。谢谢,这是代码,它在weatherapp.boomersplaydry.com上直播 index.html <!doctype html> <html> <

我正在做FreeCodeCamp课程,我正在尝试构建一个天气应用程序。我发现了一个关于如何通过地理定位获得纬度和经度的很好的教程。但现在,当我尝试运行应用程序时,它似乎没有检索ajax数据供我解析。我在本地尝试并将其移动到主机,我想这可能就是它,但现在我在html的第一行遇到了一个奇怪的错误,我没有看到任何错误。谢谢,这是代码,它在weatherapp.boomersplaydry.com上直播

index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Weather APP</title>
<link rel="stylesheet" href="style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src='script.js'></script>
</head>
<body>
<div id="forecast">
<h1>Weather at <span id="location"> </span></h1>
<!-- <div id="imgdiv">
<img id="img" src=""/> -->
</div>
<p>It is currently <span id="temp"> </span>F with <span id="desc"> </span></p>
<p>Wind: <span id="wind"></span></p>
</div>
</body>
</html>

因为您将异步调用视为同步调用。Ajax调用需要位于getCurrentPosition的成功回调中。在返回at和lng之前,您正在构建Ajax url。

您在
success
回调之外使用在Ajax响应中初始化的变量。您应该在回调中使用它们,因为它们是异步创建的:

$.ajax({
    url : Weather,
    dataType : 'jsonp',
    success : function(data) {
        var location =data['location']['city'];
        var temp = data['current_observation']['temp_f'];
        var img = data['current_observation']['icon_url'];
        var desc = data['current_observation']['weather'];
        var wind = data['current_observation']['wind_string'];
        $('#location').html(location);
        $('#temp').html(temp);
        $('#desc').html(desc);
        $('#wind').html(wind);
    }
});

这确实帮助我编写了html,但我的ajax调用仍然没有检索页面供我解析?您收到了哪个ajax响应?Nico我已经解决了。非常感谢你的帮助!
$.ajax({
    url : Weather,
    dataType : 'jsonp',
    success : function(data) {
        var location =data['location']['city'];
        var temp = data['current_observation']['temp_f'];
        var img = data['current_observation']['icon_url'];
        var desc = data['current_observation']['weather'];
        var wind = data['current_observation']['wind_string'];
        $('#location').html(location);
        $('#temp').html(temp);
        $('#desc').html(desc);
        $('#wind').html(wind);
    }
});