简体   繁体   English

使用JavaScript消费SOAP WebService

[英]Consuming SOAP WebService with JavaScript

I'm trying to consume a SOAP (.net) WebService with JavaScript but the responseText and the responseXML are null. 我正在尝试使用JavaScript使用SOAP(.net)WebService,但是responseText和responseXML为null。 I tried running in another browser(chrome, firefox, IE) but that didn't solve it. 我尝试在其他浏览器(chrome,firefox,IE)中运行,但是并不能解决问题。

function MButton1Click(event) {
    sendDataAsXML_SOAP();
}


function sendDataAsXML_SOAP() {
    var req_params = "",
        url = "",
        number = 0,
        type = "";
    /* Configure Parameters */
    url = "http://wp.art.br/FriendNet/Principal.asmx";
    var user = document.getElementById("MTextArea1").value;
    var ajaxRequest;
    req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    req_params = req_params + "<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/\">";
    req_params = req_params + "  <soap:Body>";
    req_params = req_params + "    <TesteDeTexto xmlns=\"http://tempuri.org/\">";
    req_params = req_params + "      <pTexto>" + user + "</pTexto>";
    req_params = req_params + "    </TesteDeTexto>";
    req_params = req_params + "  </soap:Body>";
    req_params = req_params + "</soap:Envelope>";
    /* Send XML/SOAP Request To Web Service Using Browser's Javascript DOM */
    var xmlHTTP;
    if (window.XMLHttpRequest) {
        xmlHTTP = new window.XMLHttpRequest; //For browsers other than ie
    } else {
        try {
            xmlHTTP = new ActiveXObject("MSXML2.XMLHTTP.3.0"); //for ie
        } catch (ex) {}
    }
    xmlHTTP.open("POST", url, true);
    xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/TesteDeTexto");
    xmlHTTP.onreadystatechange = receiveXML_SOAPData;
    xmlHTTP.send(req_params);
}

function receiveXML_SOAPData() {
    if (ajax_request.readyState == 4) {
        if (ajax_request.status == 200 || ajax_request.status == 0) {
            /* Parse The Response Data */
            alert(ajax_request.responseText);
            alert(ajax_request.responseXML);
            alert("sucesso");
        }
    }
}

You try to use a ajax_request in your receiveXML_SOAPData function which is undefined . 您尝试在undefined receiveXML_SOAPData函数中使用ajax_request You should have gotten an exception from that, check your error console. 您应该从中得到一个例外,检查您的错误控制台。

The ajaxrequest variable in the sendDataAsXML_SOAP function is a) not used and b) local to that function - it would not work. sendDataAsXML_SOAP函数中的ajaxrequest变量是a)未使用和b)该函数本地的-它不起作用。

Use the this keyword in the receiveXML_SOAPData function to reference the XHR object instead. 请在receiveXML_SOAPData函数中使用this关键字来引用XHR对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM