尝试在 AngularJS 中使用 jsonp 使用跨域 Web 服务时获取"Uncaught SyntaxError: Unexpected token :"



您好,我是Angular JS的新手。我正在尝试食用旨在以JSON格式返回数据的网络服务,我正在Localhost(WAMP)上的本地JS文件中调用它。Web服务是在.NET平台中设计的。我在击中跨域网络服务时使用了JSONP。

我的angularjs代码是这样的:

var app = angular
                .module("myModule",[])
                .controller("myController",function($scope,$http,$sce,$log){
                    var url = "http://somevalid_api_url";
                    url=$sce.trustAsResourceUrl(url);
                    $http.jsonp(url, {jsonpCallbackParam: 'callback'})
                    .then(function(data){
                        console.log(data);
                    });
                });

API的响应是这样的:

{  
   "deviceId":null,
   "id":1,
   "isNewUser":"u0000",
   "message":"Sucess",
   "playlistId":0,
   "userId":0,
   "privacyPolicy":"<p style="box-sizing: border-box; margin: 0px 0px 12.5px; color: rgb(0, 0, 0); font-family: wf_segoe-ui_normal, Tahoma, Verdana, Arial, sans-serif; font-size: 16px;">u000du000au0009Your privacy is important to us. This privacy statement explains what personal data we collect from you and how we use it. We encourage you to read the summaries below and to click on &quot;Learn More&quot; if you&#39;d like more information on a particular topic.</p>u000du000a<p style="box-sizing: border-box; margin: 0px 0px 12.5px; color: rgb(0, 0, 0); font-family: wf_segoe-ui_normal, Tahoma, Verdana, Arial, sans-serif; font-size: 16px;">u000du000au0009The product-specific details sections provide additional information relevant to particular Microsoft products. This statement applies to the Microsoft products listed below, as well as other Microsoft products that display this statement. References to Microsoft products in this statement include Microsoft services, websites, apps, software and devices.</p>u000du000a",
   "termAndCondition":"<p>u000du000au0009<span style="color: rgb(51, 51, 51); font-family: Georgia, &quot;Times New Roman&quot;, serif; font-size: 18px;">Terms and Conditions are a set of rules and guidelines that a user must agree to in order to use your website or mobile app. It acts as a legal contract between you (the company) who has the website or mobile app and the user who access your website and mobile app.</span></p>u000du000a",
   "termsId":1
}

在浏览器的控制台中获取以下错误:

Uncaught SyntaxError: Unexpected token :

当我单击错误时,我会收到以下详细信息:控制台中的详细信息

这意味着它在" deviceId"之后立即从colon(:)出现了一些问题。

我尝试了以下链接中的解决方案,但对我没有工作:

解析JSONP $ HTTP.JSONP()响应Angular.js

使用$ HTTP服务的AngularJS跨域请求

跨域$ http请求angularjs

angularjs untured syntaxerror:意外的令牌:

已经尝试了两个版本的Angualr JS v1.6.2 v1.5.1.5.1 ,但两者都失败了。

i Dint从以下解决方案中理解了任何内容:AngularJS JSONP不起作用

,如果有人能帮助或指导我,我真的很感激。

,因为它不是JSONP格式,所以JSONP响应将是这样的:

mycallback({ user: 'wang' });

不喜欢普通JSON:

{ user: 'wang' }

您将需要服务器端进行JSONP,而不仅仅是前端编码。

相关内容

  • 没有找到相关文章

最新更新