Why is this JSONP feed throwing “Unexpected Token” error?

北战南征 提交于 2019-12-20 04:24:08

问题


I'm trying to grap this remote JSONP feed via jQuery.

Every time I try, I get an error of "Uncaught SyntaxError: Unexpected token (" or similar.

Am I doing something wrong or is something in the JSONP data formatted/escaped incorrectly?

Fiddle: http://jsfiddle.net/callmeed/d3tSX/2/

var _event_url = "http://calvaryslo.onthecity.org/plaza/*events*?format=json";
$.ajax({
    type: 'GET',
    url: _event_url,
    dataType: 'jsonp',
    success: function(msg) {
        alert(msg.length);
    }
});​

回答1:


Because this feed returns JSON, not a javascript function call:

[{Query172031345640518702567_1334079878875({...

But it should be:

Query172031345640518702567_1334079878875({...

I don't know if this service is under your control and you can changed it, or you just need to add some params to the URL (check the manual).

Also the content type of the response should be application/javascript, not text/json.




回答2:


If you use dataType: 'jsonp' JQuery appends a callback url parameter to the request. The value of this parameter must be the function name of the callback function the server returns. As Eugene Retunsky already mentioned this is formatted incorrectly, also it seems that the server code is omitting the first character of the function name so jQuery_674... becomes Query_674... which will not work.



来源:https://stackoverflow.com/questions/10093697/why-is-this-jsonp-feed-throwing-unexpected-token-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!