【问题标题】:Jquery $.getJSON is not working Chrome and Firefox but it works in IEJquery $.getJSON 不适用于 Chrome 和 Firefox,但它适用于 IE
【发布时间】:2012-03-07 09:55:00
【问题描述】:

我正在尝试在我的网站中使用 yahoowheather api,因此我编写了一个简单的脚本,如下所示:

<button>show</button><br />

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://weather.yahooapis.com/forecastjson?w=2344116&u=c';
var query;
    $('button').click(function(){
        $.getJSON(url, function(json) {
        alert(json.units.temperature);
        });
    });
});
</script>

它适用于 IE(我正在弹出警报),但它不适用于 chrome 和 firefox。另外,我验证了 JSON 输出;

{
    "units": {
        "temperature": "C",
        "speed": "km/h",
        "distance": "km",
        "pressure": "mb"
    },
    "location": {
        "location_id": "TUXX0014",
        "city": "Istanbul",
        "state_abbreviation": "*",
        "country_abbreviation": "TU",
        "elevation": 92,
        "latitude": 41.01,
        "longitude": 28.95
    },
    "wind": {
        "speed": 10,
        "direction": "NE"
    },
    "atmosphere": {
        "humidity": "34",
        "visibility": 9.99,
        "pressure": 1016,
        "rising": "steady"
    },
    "url": "http://weather.yahoo.com/forecast/TUXX0014.html",
    "logo": "http://l.yimg.com/a/i/us/nt/ma/ma_nws-we_1.gif",
    "astronomy": {
        "sunrise": "06:29",
        "sunset": "18:03"
    },
    "condition": {
        "text": "Fair",
        "code": "34",
        "image": "http://l.yimg.com/a/i/us/we/52/34.gif",
        "temperature": 9
    },
    "forecast": [
        {
            "day": "Today",
            "condition": "Mostly Sunny",
            "high_temperature": 8,
            "low_temperature": 2
        },
        {
            "day": "Tomorrow",
            "condition": "Mostly Cloudy",
            "high_temperature": 8,
            "low_temperature": 3
        }
    ]
}

我们将不胜感激。

最好的问候。

奥斯曼。

【问题讨论】:

  • 在 jsFiddle 中尝试这个在 Chrome 和 Firefox 中给我一个 Access-Control-Allow-Origin 失败。

标签: jquery json getjson


【解决方案1】:

我试过了:http://jsfiddle.net/arXt2/ 我的错误日志说:

XMLHttpRequest cannot load http://weather.yahooapis.com/forecastjson?w=2344116&u=c. 
Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

你想要的是cross side scripting。见How to make an ajax with JQuery with cross site scripting?

【讨论】:

  • 似乎我需要编写服务器端脚本来启用代理。我正在研究共享点环境。我在 page_load 函数中添加了以下代码,但什么也没发生。我仍然有问题:( Response.AppendHeader("Access-Control-Allow-Origin", "*");
【解决方案2】:

跨源浏览器问题。

欲了解更多信息:http://enable-cors.org/

试试 Using the Yahoo Weather API with JSON and the script tag

【讨论】:

  • 卢卡感谢您的快速回复。我尝试了使用带有 JSON 和脚本标签的 Yahoo Weather API 编写的脚本,但问题仍然存在。奇怪的是我可以用类似的脚本解析来自 twitter 或 flickr 的数据
  • 似乎我需要编写服务器端脚本来启用代理。我正在研究共享点环境。我在 page_load 函数中添加了以下代码,但什么也没发生。我仍然有问题:( Response.AppendHeader("Access-Control-Allow-Origin", "*");
猜你喜欢
  • 2013-04-22
  • 1970-01-01
  • 2014-07-23
  • 2019-10-26
  • 2013-05-03
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2012-01-27
相关资源
最近更新 更多