码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-06 19:38:13      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   java   for   sp   数据   

<!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 = {};
                if (callback) {
                    if (xmlhttp.responseText != "") {
                        var json = eval("(" + xmlhttp.responseText + ")");
                        returnData = json;
                    }
                    callback(returnData);
                }
            }  
        }  
    };
    //获取字符串长度 
    var getlen = function(str) {
        var bytesCount = 0;
        for (var i = 0; i < str.length; i++) {
            var c = str.charAt(i);
            if (/^[u0000-u00ff]$/.test(c)) {
                bytesCount += 1;
            } else {
                bytesCount += 2;
            }
        }
        return bytesCount;
    }; 
    //获取数据串 
    var getData = function(method, str) {
        var data;
        data = "<?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 = getData(method, data);
            callback = call;
            setRequest(method, data);
        }
    }
} ();

function loadData(){
    var enterName=GetQueryString("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>

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

第一个解决方法bubuko.com,布布扣

其它方法就是添加web引用

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

标签:blog   http   io   ar   os   java   for   sp   数据   

原文地址:http://www.cnblogs.com/jameslif/p/4079472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!