jQuery JsonP 返回“Uncaught SyntaxError: Unexpected token :”AngularJS - routingnumbers.info

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26262235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:14:43  来源:igfitidea点击:

JsonP returning "Uncaught SyntaxError: Unexpected token :" AngularJS - routingnumbers.info

jqueryjsonangularjsjsonpangular-http

提问by dannypaz

I've researched this question a ridiculous amount and would hope that someone can help diagnose what is wrong.

我对这个问题进行了大量研究,希望有人能帮助诊断出问题所在。

I've already tried looked at the following SO questions:(SO wouldn't let me post more than 2 links due to reputation, so i've just included the paths)

我已经尝试查看以下 SO 问题:(由于声誉,因此我不会让我发布超过 2 个链接,所以我只包含了路径)

  1. questions/16344933/angularjs-jsonp-not-working/16352746#16352746
  2. questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery
  3. questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp
  1. 问题/16344933/angularjs-jsonp-not-working/16352746#16352746
  2. 问题/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery
  3. 问题/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp

Among many others......

在许多其他人中......

Things I have tried:I've added &callback=JSON_CALLBACKto the end of the url. I've changed config settings such as responseType: 'JSON'. I've also rearranged the http.jsonp request multiple times to make sure it was not something programmatic/textual (http({}) & http.jsonp)

我尝试过的事情:我在 url 的末尾添加了&callback=JSON_CALLBACK。我已经更改了配置设置,例如responseType: 'JSON'。我还多次重新排列 http.jsonp 请求,以确保它不是编程/文本(http({}) & http.jsonp

Here is what i'm trying to do:Grab information from routingnumbers.info/api/using an angular jsonp request (server does not allow CORS). I am able to make the request successfully with jQuery, but i'm unable to make it successfully with angular.

这是我正在尝试做的事情:使用 angular jsonp 请求(服务器不允许 CORS) 从routingnumbers.info/api/获取信息。我可以使用 jQuery 成功发出请求,但是我无法使用 angular 成功发出请求。

Here is the corresponding test fiddle:http://jsfiddle.net/dqcpa/14/

这是相应的测试小提琴:http : //jsfiddle.net/dqcpa/14/

As you can see, I receive two errors:

如您所见,我收到两个错误:

  1. Resource interpreted as Script but transferred with MIME type text/plain: "https://routingnumbers.herokuapp.com/api/data.json?rn=071000013&callback=angular.callbacks._0". angular.min.js:97
  2. Uncaught SyntaxError: Unexpected token :
  1. 资源被解释为脚本但使用 MIME 类型 text/plain 传输:“ https://routingnumbers.herokuapp.com/api/data.json?rn=071000013&callback=angular.callbacks._0”。angular.min.js:97
  2. 未捕获的语法错误:意外标记:

But if you check the response in chrome devtools - NETWORK, it is correct: Though I do know that jsonp will return the response inside of jsonpfunction({ "MyJson": "Data"}) which is where it is getting hung up at.

但是,如果您检查 chrome devtools - NETWORK 中的响应,它是正确的:虽然我知道 jsonp 将返回 jsonpfunction({ "MyJson": "Data"}) 内的响应,这是它被挂断的地方。

Here is the original code:

这是原始代码:

//$scope.number = '071000013';
var routingApiUrl = 'https://routingnumbers.herokuapp.com/api/data.json?rn=' + $scope.number;
$http({
    method: 'jsonp',
    url: routingApiUrl + '&callback=JSON_CALLBACK',
    responseType: "json"
}).
success(function(data){
    console.log('Success: ' + data);
}).
error(function(data){
    console.log('Error: ' + data);
});

Has anyone used this API with angular? I'm assuming that there may be something I can do (sans jquery) to modify headers, but I have not been able to find any information. I'm also thinking that it could be a server issue (although, if it is working correctly in jquery, that wouldn't be the issue). Maybe this could be something with HTTPS

有没有人用 angular 使用过这个 API?我假设我可以做一些事情(sans jquery)来修改标题,但我找不到任何信息。我还认为这可能是服务器问题(虽然,如果它在 jquery 中正常工作,那不是问题)。也许这可能与 HTTPS 有关

TL:DR- Angular JSONP request is not working, but with the same URL, the jQuery JSONP request is working. Referencing the above code, what am I missing?

TL:DR- Angular JSONP 请求无效,但使用相同的 URL,jQuery JSONP 请求有效。参考上面的代码,我错过了什么?

Any help would be awesome!

任何帮助都是极好的!

EDIT:Some punctuation and stuff.

编辑:一些标点符号和东西。

采纳答案by Raymond Ativie

acoording to this question: How to get rid of Uncaught SyntaxError: Unexpected token :try adding angular.callbacks._0 (JSON)around your server side json call. it worked for me

根据这个问题: 如何摆脱未捕获的语法错误:意外令牌:尝试angular.callbacks._0 (JSON)在服务器端 json 调用周围添加。它对我有用

This was the plunker http://plnkr.co/edit/oX2UQRBA41FIHpwAP6AA

这是 plunker http://plnkr.co/edit/oX2UQRBA41FIHpwAP6AA