전체 페이지뷰

2013년 5월 29일 수요일

jquery post cross domain problem

In IIS I tried to receive the json result with jquery post method through the tomcat servelt url
But except IE 10 All Brower occurred the error !

I found the some solution with googling !

First Add response.setHeader in Tomcat servlet setting

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET,POST");
response.setHeader("Access-Control-Max-Age", "360");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");


But this solution could access at the crome and firefox browser

IE 7,8,9 didn't receive the json result with cross domain 

Second Add  javascript command at client side call javascript 

jQuery.support.cors = true;

this script solved the problem only IE7,8 brower

I definitely needed the solution IE7+

I found the jsonp and

jqery call side :

$.getJSON("http://ip:8080/servletURL?&callback=?", function(data) {
        alert(data);
});

servlet response side :

JSONObject obj = new JSONObject();
out.write(callBack + "(" + obj.toString() + ")");

result  sysout : 
jQuery17202085590819060571_1369879370386(["TEST","query","knowledge","kdb","CASE"])

result alert : ["TEST","query","knowledge","kdb","CASE"]



I had troubled with cross domain problem because each brower showed different error

댓글 없음:

댓글 쓰기