2014-02-11 8 views
0

다음 코드는 CORS 프로토콜을 사용하여 요청을 보내고 응답을 완벽하게 수신합니다. 'application/x-www-form-urlencoded'대신 'application/json'이되도록 content-type 헤더를 변경하고 싶습니다. 그러나 콘텐츠 형식을 'application/json'으로 바꾸면 요청이 실패합니다. 브라우저에서 요청을 검사하면 내용 유형 헤더가 변경되지 않고 제거 된 것을 볼 수 있습니다. 위의 코드에서AngularJS Content-Type 헤더가 추가되지 않았습니다.

var servicesApp = angular.module('services', []) 

    .config(function($httpProvider){ 

     /* Enable CORS */ 
     $httpProvider.defaults.useXDomain = true; 
     delete $httpProvider.defaults.headers.common['X-Requested-With']; 
}) 


.factory('model', function($http) { 

     var testJSON = '{"employees": [ {"firstName":"John", "lastName":"Doe"} ] }'; 

     $http({ 
      url: 'http://127.0.0.1:8081/controller/UIController', 
      method: "POST", 
      params: {"path" : "save"}, 
      data: "message= " + testJSON, 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 

     }).success(function (data, status, headers, config) { 
      alert(data.ExampleForm[1]); 

     }).error(function (data, status, headers, config) { 
      alert(status); 
     }); 

요청

Request URL:http://127.0.0.1:8081/controller/UIController?path=save 

Request Method:POST 

Status Code:200 OK 

Request Headersview source 

Accept:application/json, text/plain, */* 

Accept-Encoding:gzip,deflate,sdch 

Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 

Connection:keep-alive 

Content-Length:67 

Content-Type:application/x-www-form-urlencoded 

Host:127.0.0.1:8081 

Origin:http://127.0.0.1:8020 

Referer:http://127.0.0.1:8020/TwineClient/src/index.html 

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36 

Query String Parametersview sourceview URL encoded 

path:save 

Form Dataview sourceview URL encoded 

message: {"employees": [ {"firstName":"John", "lastName":"Doe"} ] } 

Response Headersview source 

Access-Control-Allow-Headers:Content-Type 

Access-Control-Allow-Methods:POST 

Access-Control-Allow-Origin:* 

Content-Length:51 

Content-Type:application/json;charset=ISO-8859-1 

Date:Tue, 11 Feb 2014 16:15:00 GMT 

Server:Apache-Coyote/1.1 

때 '응용 프로그램/X-형태-urlencoded로는 WWW' '응용 프로그램/JSON'로 대체된다

Request URL:http://127.0.0.1:8081/controller/UIController?path=save 

Request Method:OPTIONS 

Status Code:200 OK 

Request Headersview source 

Accept:*/* 

Accept-Encoding:gzip,deflate,sdch 

Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 

Access-Control-Request-Headers:accept, content-type 

Access-Control-Request-Method:POST 

Connection:keep-alive 

Host:127.0.0.1:8081 

Origin:http://127.0.0.1:8020 

Referer:http://127.0.0.1:8020/TwineClient/src/index.html 

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36 

Query String Parametersview sourceview URL encoded 

path:save 

Response Headersview source 

Allow:GET, HEAD, POST, TRACE, OPTIONS 

Content-Length:0 

Date:Tue, 11 Feb 2014 16:11:02 GMT 

Server:Apache-Coyote/1.1 
+1

이것은 잠재적으로 CORS 요청 일 수 있으므로 HTTP 요청과 응답의 헤더와 본문을 게시 할 수 있습니까? –

+0

... 사용중인 브라우저 또는 브라우저에 대한 설명 –

+0

두 가지 모두에 대해 http 요청과 응답을 추가했습니다. Google 크롬에서 가져온 것입니다. –

답변

1

콘텐츠 유형을 application/json으로 변경하면 브라우저가 프리 플라이트 (OPTIONS) 요청을 보냅니다. 이는 CORS 사양에 따라 필수 단계입니다. application/json을 요청한 content-type으로 사용하려면 서버가이 프리 플라이트를 제대로 인식해야합니다. CORS에 대한 자세한 내용은 https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS을 참조하십시오.

안녕하십니까. 여기에 비슷한 질문에 대답했습니다 : WebAPI 2 - CORS not working with contentType application/json.

관련 문제