• 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 = {};
                    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>
    

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

    第一个解决方法

    其它方法就是添加web引用

  • 相关阅读:
    Nginx学习总结(一)
    zabbix3.4.8配置自动发现主机并监控
    Windows server 2012/2016系统安装zabbix3.2客户端
    CentOS7.6系统安装zabbix3.4.8客户端
    一个小爬虫的整体解决方案
    如何通过一个立方体搭建一栋楼
    用Scrapy框架开发的一个爬虫项目
    寻找替代imagemin更好的插件
    原生和es6复杂数组去重的方法
    javascript关于对象或者数组深克隆的写法
  • 原文地址:https://www.cnblogs.com/jameslif/p/4079472.html
Copyright © 2020-2023  润新知