2017-02-28 1 views
1

안녕하세요 저는 Angular j를 처음 사용합니다. JSON 형식으로 데이터를 반환하도록 설계된 웹 서비스를 사용하려고합니다. localhost (WAMP)의 로컬 js 파일에서 호출합니다. Webservice는 .net 플랫폼으로 설계되었습니다. jsonp를 크로스 도메인 웹 서비스로 사용하고 있습니다.AngularJS에서 jsonp를 사용하여 크로스 도메인 웹 서비스를 사용하려고 할 때 "Uncaught SyntaxError : Unexpected token :"이 발생했습니다.

내 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 :

두에 시도 해 봤나 그 반대의 Angualr Js v1.6.2v1.5.1의 경우 모두 실패했지만 모두 실패했습니다.

I의 힘은 솔루션 다음에서 아무것도 이해 : 사람이 돕거나이를 통해 저를 인도 할 수 있다면 는 Angularjs JSONP not working

정말 감사합니다.

+0

오류는 응답 형식이 JSONP가 아닌 JSON이며 상호 교환 할 수 없기 때문에 발생합니다. 상호 도메인 요청을 할 때 '액세스 제어 헤더 없음'오류를 해결하기 위해이 작업을 수행 한 것 같습니다. –

+0

@ Rory McCrossan 예. Webservice는 JSON을 반환합니다. 내 호출 스크립트가 내 로컬 컴퓨터에 있고 웹 서비스가 내 로컬 컴퓨터가 아닌 일부 서버에 있기 때문에 get 메서드를 사용하여 공격 할 수 없었기 때문에 많은 기사를 읽었고 jsonp를이 스크립트로 사용해야한다는 것을 알았습니다. 도메인 간 시나리오입니다. 그래서 더 좋은 제안? –

+0

서버에 액세스 할 수있는 경우 CORS 헤더를 포함하도록 응답을 변경해야 도메인 간 요청이 허용됩니다. 서버에 액세스 할 수없는 경우 JS 코드에서이 API에 대한 요청을 수행 할 수 없으므로 PHP 또는 C#과 같은 서버 측 언어로 처리해야합니다. –

답변

1

그것이 JSONP 형식 아니니까하는 JSONP 응답은 다음과 같이 될 것입니다 :

mycallback({ user: 'wang' }); 

정상적인 JSON 좋아하지 :

{ user: 'wang' } 

당신은 JSONP를 할 수있는 서버 측이 필요합니다을, 프런트 엔드 코딩뿐 아니라

관련 문제