Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
如何从脱机访问联机JSON文件?(javascript)_Javascript_Ajax_Xmlhttprequest - Fatal编程技术网

如何从脱机访问联机JSON文件?(javascript)

如何从脱机访问联机JSON文件?(javascript),javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我正在为我的学校项目制作一个天气网站。这是我用来获取JSON数据的代码: $.getJSON("http://api.wunderground.com/api/<apikey>/conditions/q/" + wlocation + ".json", function(data){ alert(data); }); 在对这个错误做了一些研究之后,听起来我可能需要创建一个web服务器。然而,对于该项目,我们需要将其作为.html和其他“web文件”的文件夹提交。有没有其他方

我正在为我的学校项目制作一个天气网站。这是我用来获取JSON数据的代码:

$.getJSON("http://api.wunderground.com/api/<apikey>/conditions/q/" + wlocation + ".json", function(data){
    alert(data);
});
在对这个错误做了一些研究之后,听起来我可能需要创建一个web服务器。然而,对于该项目,我们需要将其作为.html和其他“web文件”的文件夹提交。有没有其他方法可以做到这一点,或者我必须制作一个web服务器?这个项目很快就要到期了,所以非常感谢您的帮助

是的,你可以用这个

我不确定WundergroundWeatherAPI是否在JSON中有某种回调。但是如果他们真的这样做了,jQuery
getJSON
甚至都支持JSONP

好像你遇到了。

是的,你可以用这个

我不确定WundergroundWeatherAPI是否在JSON中有某种回调。但是如果他们真的这样做了,jQuery
getJSON
甚至都支持JSONP


似乎您遇到了。

这是您在原始帖子()中提供的链接中的代码示例。他们使用JSONP。是的,正如@antyrat所说,这是一个CORS问题

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
  $.ajax({
      url : "http://api.wunderground.com/api/Your_Key/geolookup/conditions/q/IA/Cedar_Rapids.json",
      dataType : "jsonp",
      success : function(parsed_json) {
          var location = parsed_json['location']['city'];
          var temp_f = parsed_json['current_observation']['temp_f'];
          alert("Current temperature in " + location + " is: " + temp_f);
      }
  });
});
</script>

jQuery(文档).ready(函数($){
$.ajax({
url:“http://api.wunderground.com/api/Your_Key/geolookup/conditions/q/IA/Cedar_Rapids.json",
数据类型:“jsonp”,
成功:函数(已解析的_json){
var location=parsed_json['location']['city'];
var temp_f=parsed_json['current_observation']['temp_f'];
警报(“位置+”中的当前温度为:“+temp_f”);
}
});
});

这是您在原始帖子()中提供的链接中的代码示例。他们使用JSONP。是的,正如@antyrat所说,这是一个CORS问题

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
  $.ajax({
      url : "http://api.wunderground.com/api/Your_Key/geolookup/conditions/q/IA/Cedar_Rapids.json",
      dataType : "jsonp",
      success : function(parsed_json) {
          var location = parsed_json['location']['city'];
          var temp_f = parsed_json['current_observation']['temp_f'];
          alert("Current temperature in " + location + " is: " + temp_f);
      }
  });
});
</script>

jQuery(文档).ready(函数($){
$.ajax({
url:“http://api.wunderground.com/api/Your_Key/geolookup/conditions/q/IA/Cedar_Rapids.json",
数据类型:“jsonp”,
成功:函数(已解析的_json){
var location=parsed_json['location']['city'];
var temp_f=parsed_json['current_observation']['temp_f'];
警报(“位置+”中的当前温度为:“+temp_f”);
}
});
});

@Nikola Umm。。。你读过我提供的维基文章吗?这里有你需要的所有代码。我不认为有什么像教程,因为JSONP只是带有回调函数的JSON,就这样!因为您使用的是jQuery-help。如果URL包含字符串“callback=?”(或类似,由服务器端API定义),则从“JSONP”开始,请求被视为JSONP。有关更多详细信息,请参阅$.ajax()中关于JSONP数据类型的讨论。@antyrat Now我觉得很傻。然而,我确实在@Nikola Umm找到了一个小教程。。。你读过我提供的维基文章吗?这里有你需要的所有代码。我不认为有什么像教程,因为JSONP只是带有回调函数的JSON,就这样!因为您使用的是jQuery-help。如果URL包含字符串“callback=?”(或类似,由服务器端API定义),则从“JSONP”开始,请求被视为JSONP。有关更多详细信息,请参阅$.ajax()中关于JSONP数据类型的讨论。@antyrat Now我觉得很傻。然而,我确实在网站上找到了一些教程