weixin_33743880 2014-07-22 10:04 采纳率: 0%
浏览 81

获取JSON意外令牌

I am trying to get JSON file with JSONP:

function getJson() {
    var url = 'http://www.oref.org.il/WarningMessages/alerts.json?callback=?';
    $.getJSON(url, function(data) {
        checkJson(data);
    });
    setTimeout(arguments.callee, 2000);
}
getJson();

The problem is that I am getting this error:

Uncaught SyntaxError: Unexpected token :

So I searched online and I found few solutions, but non of them worked.

I tried to use Ajax instead and I tried to replace $.getJSON with $.parseJSON but I got this error:

Uncaught SyntaxError: Unexpected token h 

The JSON file:

{ "id" : "1406032249536", "title" : "test", "data" : [] }

  • 写回答

1条回答 默认 最新

  • ??yy 2014-07-22 10:26
    关注

    Because you have a callback=? in your URL, you are making a JSONP request.

    The response for a JSONP request must be a JavaScript application. It should consist of a function call (to the function defined by (in PHP terms) $_GET['callback']) with one argument (the data you are asking for).

    You are getting JSON back, so the service you are trying to connect to does not support JSONP (at least not with the standard callback argument — consulting their documentation might give you a different argument to use).


    You could make a standard XMLHttpRequest (by taking the callback=? out), but you said (in comments) that that gives you a complaint about Access-Control-Allow-Origin not giving your site permission to access it.


    If you control the JSON endpoint, you could change it so it supports either JSONP or CORS (which is what provides Access-Control-Allow-Origin). It sounds like you do not control that endpoint though, so that isn't an option.


    This leaves you with one option: Use a proxy.

    Make the Ajax request to your own server. Have a server side program running on your server make the request to the JSON endpoint and return the data.

    You could also look at using a third party proxy, such as YQL, but that will probably suffer from the geographic restrictions you mentioned in another comment on the question.

    评论

报告相同问题?

悬赏问题

  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义