简体   繁体   English

jQuery Ajax-意外令牌:

[英]jQuery Ajax - Unexpected Token :

I'm trying to make an AJAX GET request to this URL and process the JSON response. 我正在尝试对此URL进行AJAX GET请求并处理JSON响应。 If you visit that URL directly, it works. 如果您直接访问该URL,它将起作用。 I've also tried running it through Hurl It , and it works too. 我也尝试通过Hurl It运行可以工作

I'm there for at a loss as to why I can't make a JSON / JSONP request to it through jQuery. 对于为什么我无法通过jQuery向其发出JSON / JSONP请求,我感到茫然。

I initially tried to make a JSON response but get: 我最初尝试进行JSON响应,但得到:

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 因此,我尝试了JSONP,并获得了

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

Line 1 is the entire JSON response, in one line, but not wrapped in a callback. 第1行是整个JSON响应,在一行中,但没有包装在回调中。 Hence, it seems this API does not support JSONP. 因此,似乎此API不支持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还是服务器端代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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