使用带有JSON和脚本标记的Yahoo Weather API [英] Using the Yahoo Weather API with JSON and the script tag

查看:287
本文介绍了使用带有JSON和脚本标记的Yahoo Weather API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用JavaScript获取雅虎天气。我最初做了一个代理,但发现笨拙。

I'm trying to get the Yahoo Weather with JavaScript. I originally made a proxy, but found that clumsy.

所以可以从 http://weather.yahooapis.com/forecastjson?w=9807 ,我知道脚本标签可以避免相同域的限制,但我得到语法错误。

So can get the JSON response from http://weather.yahooapis.com/forecastjson?w=9807, and I know that the script tag can avoid the same-domain restrictions, but I'm getting a syntax error.

Yahoo的JSON响应未填充;我已经有回调工作,但浏览器没有正确地解释JSON。

Yahoo's JSON response isn't padded; I've got the callback working but the browser isn't interpreting the JSON properly.

我见过很多例子如何读取jahoo天气JSON数据与Jquery ajax 但它是如此奇怪,因为所有那些给我的跨域错误。

I've seen many examples like How to read yahoo weather JSON data with Jquery ajax but it's so weird because all those give me the cross-domain error.

任何人都可以帮助我这个?跨域,雅虎天气,没有特殊的服务器或YQL或类似的东西。

Can anyone help me with this? Cross domain, yahoo weather, without special servers or YQL or anything like that. Something that just works out of the box.

推荐答案

如果你期望 JSON-P 那么你需要添加一个回调函数名到查询。使用jQuery,这总是。 jQuery将使用随机生成的函数名称替换它:

If you're expecting JSON-P then you need to add a callback function name to the query. With jQuery, this is always ?. jQuery will substitute it with a randomly generated function name:

var query = escape('select item from weather.forecast where location="CAXX0518"'),
    url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=?"; 

$.getJSON(url, function(data) {
  console.log( data );
});

这篇关于使用带有JSON和脚本标记的Yahoo Weather API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆