JSON数据未返回-jQuery AJAX [英] JSON data not being returned - JQuery AJAX
问题描述
我正在测试此脚本,该脚本从URL检索天气数据.但是由于某种原因,我没有得到答复.我启用了跨站点.有人可以指出问题吗?
I am testing out this script which retrieves the weather data from the URL. But for some reason I am not getting the response back. I have enabled cross-site. Can someone point out the problem?
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type:"GET",
url:"http://api.openweathermap.org/data/2.5/weather?q=London",
headers: { "Accept": "application/json; odata=verbose" },
crossDomain:true,
success:function(result){
$("#div1").html(result);
}});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
推荐答案
Soultion1
如果您的服务器上有php,则在与您的页面相同的域上创建一个名为weather.php的文件,以将此代码宽度设置为
if you have php on your server create a file called weather.php width this code on the same domain as your page.
<?php
echo file_get_contents('http://api.openweathermap.org/data/2.5/weather?q='.$_GET['q']);
?>
并使用您的函数进行调用
and call it with your function
url:"weather.php?q=London",
注意:慢速但真正的ajax
Note: slow but real ajax
Soultion2
如果openweathermap.org支持回调,则可以使用jsonp
if openweathermap.org has support for callbacks you can use jsonp
注意::用<srcip></script>
标签填充页面.
Soultion3
使用nodejs代理
注意:快速&真正的ajax
Note: fast & real ajax
Soultion4
使用yql查询.
注意:最快&真正的ajax
Note: fastest & real ajax
如果您需要更多详细信息,请询问
编辑
解决方案5
使用php传递内容的快速方法
A Fast way to passtrough the content with php
<?php
function w($ch,$chunk){
echo $chunk;
ob_flush();
flush();
return strlen($chunk);
};
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$_GET['url']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_WRITEFUNCTION,w);
curl_exec($ch);
curl_close($ch);
?>
注意:比file_get_contents
&真正的ajax
Note: faster than file_get_contents
& real ajax
通过删除
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
部分,它甚至更快.
EDIT3
相对于直接而言,代理总是一个坏主意,因为您总是要读取2次数据.
proxy's are always a bad idea vs direct as you read the data always 2 times.
直接行动
询问->阅读->显示
代理动作
询问->(PHP/节点询问)->(PHP/节点读取)->(PHP/节点显示)->读取->显示
ask->(php/node ask)->(php/node read)->(php/node display)->read->display
但是在您的情况下,没有其他方法可以直接获取数据.
but in your case there is no other way to get the data directly.
我根据平均主机将yql标记为最快.
i tagged the yql as fastest based on average hosts.
诸如openweathermap之类的重要站点的大多数数据可能已经缓存在yahoo服务器上,其带宽在全球范围内都很高(并且允许跨域访问).
most of the data from important sites like openweathermap is probably already cached on the yahoo servers and the their bandwith is very high worldwide (and crossorigin is allowed).
因此,如果您的主机较慢,则需要使用php或nodejs& amp;从openweathermap读取数据然后输出的带宽有限,比Yahoo的服务器要慢99%.
so if you have a slow host that needs to read the data from openweathermap with php or nodejs & then output having a limited bandwidth it's 99% slower than on yahoo's server.
nodejs比php更快,因为如果您创建了良好的专用代理脚本,则会将数据直接存储在系统内存中.在内存中缓存数据比我所知道的要快得多.也许它比读取静态文件还要快.
nodejs is faster than php because if you create o good dedicated proxy script you store your data directly inside the system memory. Caching data inside the memory is faster than anything else i know.probaly it's even faster then reading a static file.
关键是主机输出请求的速度.
the point is how fast your host outputs the request.
这篇关于JSON数据未返回-jQuery AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!