2017-02-28 24 views
1

您好我對Angular js來說很新。我正在嘗試使用旨在以JSON格式返回數據的Web服務,我從本地主機(WAMP)上的本地js文件調用它。 Web服務是在.net平臺上設計的。 我使用了jsonp,因爲我正在實現跨域web服務。嘗試在AngularJS中使用jsonp使用跨域webservice時出現「未捕獲的SyntaxError:意外的令牌:」

我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;\">\u000d\u000a\u0009Your 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>\u000d\u000a<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;\">\u000d\u000a\u0009The 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>\u000d\u000a", 
    "termAndCondition":"<p>\u000d\u000a\u0009<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>\u000d\u000a", 
    "termsId":1 
} 

在瀏覽器的控制檯收到以下錯誤:

Uncaught SyntaxError: Unexpected token : 

當我點擊了錯誤我獲得以下信息: Image of details in console

這意味着它具有與結腸癌的一些問題(:),並立即開始「的DeviceID」之​​後。

我試圖從下面的鏈接解決方案,但並沒有爲我工作:

parsing JSONP $http.jsonp() response in angular.js

AngularJS cross-domain requests using $http service

Cross-domain $http request AngularJS

AngularJS Uncaught SyntaxError: Unexpected token :

有兩個試過VERSI Angualr Js v1.6.2v1.5.1,但兩者均失敗。

我從力下述溶液瞭解什麼: Angularjs JSONP not working

,我將非常感激,如果有人能幫助或指導我這個。

+0

錯誤是因爲響應格式是JSON而不是JSONP,它們不可互換。我想你已經這樣做了,以避免在發出你的跨域請求時的'無訪問控制頭'錯誤,對嗎? –

+0

@Rory McCrossan是的。 Webservice返回JSON。由於我的調用腳本位於本地機器上,並且Web服務位於某些服務器上,而不是本地機器上,因此我無法使用get方法來打它,因此我瀏覽了很多文章,發現我必須使用jsonp。是一個跨域場景。那麼更好的建議? –

+0

如果您有權訪問服務器,則需要將響應更改爲包含CORS標頭,以便允許跨域請求。如果您無法訪問服務器,那麼您將無法通過JS代碼向此API發出請求,您需要使用服務器端語言(如PHP或C#)來執行此操作 –

回答

1

因爲它不是一個JSONP格式,JSONP響應將是這個樣子:

mycallback({ user: 'wang' }); 

不喜歡正常的JSON:

{ user: 'wang' } 

您將需要一個服務器端做JSONP,不僅前端編碼。

相關問題