前端JS Ajax 調用Java Axis的 web服務

來源:互聯網
上載者:User
/* 此方法為Ajax 調用Java Axis的web服務,調用舉例如下:callAxisWsPost("遠程方法名", new Array("參數名1","參數名2"...), new Array(參數值1,參數值2...),                         "wsdl地址",                         "命名空間", function (result) {                             //Ajax完成後的回調處理,result為服務端返回結果                         }, function () {                             //Ajax載入時的處理,如彈出loading遮罩層,提交按鈕禁用等                         });*/function callAxisWsPost(method, variable, value, url, _Namespace, callback, loadProcess) {    function getlen(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 xmlhttp = null;    if (window.ActiveXObject) {        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    } else if (window.XMLHttpRequest) {        xmlhttp = new XMLHttpRequest();    }    var data;    data = '<?xml version="1.0" encoding="utf-8"?>';    data = 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 = data + '<soap:Body>';    data = data + '<' + method + ' xmlns="' + _Namespace + '">';    for (var i = 0; i < variable.length; i++) {        data = data + '<' + variable[i] + '>' + value[i] + '</' + variable[i] + '>';    }    data = data + '</' + method + '>';    data = data + '</soap:Body>';    data = data + '</soap:Envelope>';    xmlhttp.onreadystatechange = function () {        if (xmlhttp.readyState == 1) {            if (loadProcess)                loadProcess();        }        if (xmlhttp.readyState == 4) {            if (xmlhttp.status == 200) {                if (callback)                   callback(xmlhttp.responseText);            }        }    }    xmlhttp.Open("POST", url, true);    xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=utf-8");    xmlhttp.SetRequestHeader("Content-Length", getlen(data));    xmlhttp.SetRequestHeader("SOAPAction", _Namespace + method);    xmlhttp.Send(data);}

具體的調用舉例

                callAxisWsPost("getPoleByLineid", new Array("lineid"), new Array(record.data.transLinId),                         "http://192.168.1.129:8005/axis2/services/SimpleService?wsdl",                         "http://ws.apache.org/axis2", function (result) {                             var XmlDoc = getXmlDom(result);                             var returns = XmlDoc.getElementsByTagName("return");                             var poleStr = returns[0].childNodes[0].nodeValue;                             var poleInfoArray = poleStr.split(";");                             for (var i = 0; i < poleInfoArray.length - 1; i++) {                                 var poleInfos = poleInfoArray[i].split(",");                                 var id = poleInfoArray[i];                                 var name = poleInfos[0];                                 transStartArray[i] = [id, name];                                 transEndArray[i] = [id, name];                             }                             ComboTowerStart.clearValue();                             ComboTowerStart.store.removeAll();                             ComboTowerEnd.clearValue();                             ComboTowerEnd.store.removeAll();                             ComboTowerStart.store.loadData(transStartArray);                             var towerStartId = ComboTowerStart.store.getAt(0).get('towerStartId'); //預設選中第一個                             ComboTowerStart.setValue(towerStartId);                             ComboTowerEnd.store.loadData(transEndArray);                             var towerEndId = ComboTowerEnd.store.getAt(0).get('towerEndId'); //預設選中第一個                             ComboTowerEnd.setValue(towerEndId);                             Efs.getExt('btnLinesFlightOk').enable();                         }, function () {                             Efs.getExt('btnLinesFlightOk').disable();                         });
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.