首页 > 代码库 > XMLHttpRequest 请求java部署的webservice 跨域问题

XMLHttpRequest 请求java部署的webservice 跨域问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html>  <head>    <title>企业列表</title>  </head>      <body  >  </body></html><script  type="text/javascript">var webservice = function() {    var nameSpace = "http://ws.search.serviceinterface.ths.com";     var url = "http://192.168.0.152:8082/service/ws/jsonWebService";     //创建 xmlhttp     var xmlhttp;    var callback;    var createXmlHttp = function() {          if (window.ActiveXObject) {            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");          }          else if (window.XMLHttpRequst) {            xmlhttp = new XMLHttpRequest();          }    };    //回调处理     var setChange = function() {          if (xmlhttp.readyState == 4) {            alert(xmlhttp.status);            alert(xmlhttp.responseText);            if (xmlhttp.status == 200) {               alert(xmlhttp.responseText);                returnData = http://www.mamicode.com/{};"") {                        var json = eval("(" + xmlhttp.responseText + ")");                        returnData = http://www.mamicode.com/json;"<?xml version=\"1.0\" encoding=\"utf-8\"?>";        data += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";        data += "<soap:Body>";        data += "<"+ method + " xmlns=\"" + nameSpace + "\">";        data += "<in0>402880184877f94f0148862c84530341</in0>";        data += "<in1>8</in1>";        data += "<in2>"+ str + "</in2>";        data += "</" + method + ">";        data += "</soap:Body>";        data += "</soap:Envelope>";        return data;    };    var setRequest = function(method, data) {        createXmlHttp();        xmlhttp.onreadystatechange = setChange;        xmlhttp.open("POST", url, true);        xmlhttp.setRequestHeader("Content-Type", "json; charset=utf-8");        xmlhttp.setRequestHeader("Content-Length", getlen(data));        xmlhttp.setRequestHeader("SOAPAction", nameSpace + method);        xmlhttp.send(data);    }    return {        postWs: function(method, data, call) {            var data = http://www.mamicode.com/getData(method, data);"enterName");    var jsonStr = "{\"pager\":{\"page\":\"\",\"pageSize\":\"10\"},\"params\":[{\"FieldName\":\"STANDENTERID\",\"Value\":\"\",\"Operator\":\"=\"},{\"FieldName\":\"ENTERID\",\"Value\":\"\",\"Operator\":\"=\"},{\"FieldName\":\"ENTERCODE\",\"Value\":\"\",\"Operator\":\"=\"},{\"FieldName\":\"ENTERNAME\",\"Value\":\"%"+enterName+"%\",\"Operator\":\"like\"},{\"FieldName\":\"CORPCODE\",\"Value\":\"\",\"Operator\":\"=\"}]}";    webservice.postWs("RunJsonResult", jsonStr,    function(data) {        alert(data);    });}function GetQueryString(name) {    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");    var r = window.location.search.substr(1).match(reg);    if (r!=null) return (r[2]); return null; }  window.load=loadData();</script>

此时存在跨域问题,回报一个无权限的访问

第一个解决方法

其它方法就是添加web引用

XMLHttpRequest 请求java部署的webservice 跨域问题