新的W3C策略实现了HTTP跨域访问,还亏我找了很久的资料解决这个问题: 只需要在servlet中返回的头部信息中添加Access-Control-Allow-Origin这个既可。 比如我要开放所有我本地的跨域访问,就设置如下:response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1/*"); 这样我本地的A工程中的AJAX请求就可以跨域请求B工程中的servlet。 代码如下: HTML的JS的ajax请求: 复制代码 代码如下: /* Create a new XMLHttpRequest object to talk to the Web server */ var xmlHttp = false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } } @end @*/ if (!xmlHttp && typeof XMLHttpRequest != "undefined") { xmlHttp = new XMLHttpRequest(); } var url = "http://127.0.0.1:2012/esb/servlet/HttpClient?randomType=MIX"; xmlHttp.open("GET", url, true); //Setup a function for the server to run when it"s done xmlHttp.onreadystatechange = function(){ if (xmlHttp.readyState == 4) { var response = xmlHttp.responseText; alert(response); } } //Send the request xmlHttp.send(null);