다음 코드는 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
이것은 잠재적으로 CORS 요청 일 수 있으므로 HTTP 요청과 응답의 헤더와 본문을 게시 할 수 있습니까? –
... 사용중인 브라우저 또는 브라우저에 대한 설명 –
두 가지 모두에 대해 http 요청과 응답을 추가했습니다. Google 크롬에서 가져온 것입니다. –