프로그래밍/Node.js

Node.js- ajax로 타 도메인과 연동 문제 / CORS(Cross-Origin Resource Sharing)

가카리 2014. 12. 27. 18:52
반응형

클라이언트 소스(브라우저)

1
2
3
4
5
6
7
8
9
$.ajax({
url : "192.168.48.123/api/client",
type : "POST",
cache : false,
dataType:"json",
success: function(data) {
console.log(data);
},
});

 

오류메세지
XMLHttpRequest cannot load http://192.168.48.123/api/client. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '192.168.48.456' is therefore not allowed access.

해결방법(서버 Node.js 소스 수정)

1
2
3
4
5
6
response.type('application/json');
response.header("Access-Control-Allow-Origin" , "*")
response.json(200, {
code : code,
result : result
});

 

참고사이트

 

출처 : http://blog.redjini.com/275

반응형