local 에서 html file 을 열어서 원하는 REST API 를 이용해서 화면에 결과를 뿌려주려고 했다. 그런데 REST API 를 제공하는 Server 쪽에서 요구하는 아래의 조건에 대해 JSONP 와 local file 이라는 환경에서 가능한지 확인 해 봤다.
- Content-Type : application/json
- Request Method:GET
CORS request 에서 Content-type 이 application/json 으로 설정할 수 있을까?
CORS(Cross Origin Resource Share) 을 이용해서 다른 server 에 있는 REST API 를 이용하려고 하고 있다.
기본적으로 불가능해 보인다. CORS request 에서는 reqeust header 에 content Type 설정이 안된다.
이것은 server 에서 download 한 page 나, local 에서 실행한 page 나 동작이 같았다.
- Firefox 31.0
- Chrom 36.0.xx
$.ajax() 를 이용해서 request 를 했다.
$.ajax({
type:"POST",
url: this.REQUEST_URL,
contentType: 'application/json',
data: JSON.stringify(param),
// for receiving
jsonp: "callback",
dataType: "jsonp",
success: function (resp) {
console.log(resp);
},
error: function(){
},
complete: function () {
}
}); // end of ajax
Request Method:GET 설정이 가능할까?
이것은 jsonp 로 request 할 때 <script> 를 이용하기 때문에 안된다. 자세한 이야기는 아래 link 를 참고하자.
같은 origin 에서 request(not CORS request)
Request Method:POST
Form 으로 Content-type : application/json 이 가능할까?
그래서 혹시 Form 을 이용해서 가능할까 하고 살펴봤다. Form 관련 Spec 은 Form 에서 submit 했을 때 header 가 가질 수 있는 Content type 을 2가지중 하나를 갖는다고 한다.
- application/x-www-form-urlencoded
- multipart/form-data


댓글 없음:
댓글 쓰기