我想解决我的AJAX跨域问题。这是我在Chrome中收到的错误消息:
XMLHttpRequest无法加载http://' myaddress' / TEST / user / login / testuserid。请求标头字段Access-Control-Allow-Origin不允许使用Access-Control-Allow-Origin。
这是我的源代码
$.ajax({
crossDomain : true,
dataType : "json",
url: fullUrl,
method: method,
headers:headers,
data: body,
success: function(data, state, res){
if (data == null)
data = '';
console.log( '[[[[[[[[SUCCESS!!]]]]]]] url: ' + url + ', state:' + state + ', data : ' + JSON.stringify( data ) );
callback(data, state, res);
},
error: function(data, state){
if ( data == null )
data = '';
console.log( '[[[[[[[[[ERROR!!]]]]]]]]] url: ' + url + ', s tate:' + state + ', data : ' + JSON.stringify( data ) );
callback(data, state, null);
}
});
跨域的Servlet代码
httpRes.setHeader("Access-Control-Allow-Origin", "*");
httpRes.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
httpRes.setHeader("Access-Control-Allow-Headers", "*");
答案 0 :(得分:0)