“无效标签" jQuery和Yahoo Geolocation API的parsererror [英] "invalid label" parsererror with jQuery and Yahoo's Geolocation APIs

查看:89
本文介绍了“无效标签" jQuery和Yahoo Geolocation API的parsererror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery访问Yahoo的Geolocation API.即使我能够从他们的服务器成功检索数据,也无法使jQuery成功解析数据.我已经尝试了$ .ajax()和$ .getJSON,它们都返回相同的失败:parsererror和"invalid label".

I'm using jQuery to access Yahoo's Geolocation APIs. Even though I am able to successfully retrieve data from their servers, I cannot get jQuery to successfully parse the data. I've tried both $.ajax() and $.getJSON, each returns the same failures: parsererror and "invalid label".

通过在互联网上进行挖掘,我发现无效标签"很可能是由于JSON没有被括在括号中的结果,但是在弄清楚数据之前,我不知道如何包装数据解析.我什至不相信这是问题所在.

Through my digging on the interwebs, I've discovered that "invalid label" is likely the result of the JSON not being wrapped in parentheses, but I can't figure out how to wrap the data up, prior to it getting parsed. I'm not even convinced that's the problem.

这是我的代码:

  $(document).ready(function() {
    var url  = "http://where.yahooapis.com/geocode?q=39.0334171,-94.8320452&gflags=R&flags=JT&appid=supersecretappid&callback=?";

    $.getJSON(url, function() { alert("success"); })
      .error(function(data) { alert(JSON.stringify(data)); });

  });

仅使用$ .ajax的备用版本如下:

The alternate version using just $.ajax is as follows:

$.ajax({
  url: url,
  data: {},
  dataType: "jsonp",
  contentType: "text/plain",
  success: function(json) {
    alert("success");
  },
  error: function(x,y,z) {
    alert(JSON.stringify(x));
  }
});

非常感谢.

推荐答案

您尝试访问的API不支持JSONP.因此,由于相同来源政策的限制,您不能将其与javascript一起使用.尝试在浏览器中粘贴以下网址:

The API you are trying to access doesn't support JSONP. So you cannot use it with javascript due to same origin policy restrictions. Try pasting the following url in your browser:

http://where.yahooapis.com/geocode?q=39.0334171,-94.8320452&gflags=R&flags=JT&appid=supersecretappid&callback=foo

看看如何完全忽略callback参数?服务器返回纯JSON,而不是您需要的JSONP.解决方法是,您可以在域上编写服务器端脚本,该脚本将充当Yahoo和您的域之间的桥梁.然后,您会将AJAX请求发送到您的脚本.

See how the callback parameter is completely ignored? The server returns pure JSON, not JSONP which is what you need. As a workaround you could write a server side script on your domain that will act as a bridge between Yahoo and your domain. Then you will send the AJAX request to your script.

这篇关于“无效标签" jQuery和Yahoo Geolocation API的parsererror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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