简体   繁体   中英

jQuery Ajax - Unexpected Token :

I'm trying to make an AJAX GET request to this URL and process the JSON response. If you visit that URL directly, it works. I've also tried running it through Hurl It , and it works too.

I'm there for at a loss as to why I can't make a JSON / JSONP request to it through jQuery.

I initially tried to make a JSON response but get:

XMLHttpRequest cannot load http://hndroidapi.appspot.com/news/format/json/page/. Origin http://hackernews.dev is not allowed by Access-Control-Allow-Origin.

So therefore I took an attempt at JSONP, and get

Uncaught SyntaxError: Unexpected token :  hndroidapi.appspot.com:1

Line 1 is the entire JSON response, in one line, but not wrapped in a callback. Hence, it seems this API does not support JSONP.

Hence, is there anything I can do to solve this? I'm a little stumped unfortunately, it doesn't seem like there's a way round this.

My code is as follows:

var ajaxReq = $.ajax({
  url: "http://hndroidapi.appspot.com/news/format/json/page/",
  dataType: "jsonp" 
});
ajaxReq.done(function(d) {
  console.log("DONE", d);
});
ajaxReq.error(function(d) {
  console.log("ERROR", d);
});

Any help much appreciated!

出于安全考虑,您不能向您的网页以外的其他网站发出AJAX请求。

尝试不确定使用http://api.jquery.com/jQuery.getJSON/

该API绝对不支持JSONP(除非我缺少某些东西,对python不太熟悉): https : //github.com/glebpopov/Hacker-News-Droid-API/blob/master/Formatter.py是您唯一的选择此时是使用YQL还是服务器端代理

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM