简体   繁体   中英

Consuming SOAP WebService with JavaScript

I'm trying to consume a SOAP (.net) WebService with JavaScript but the responseText and the responseXML are null. I tried running in another browser(chrome, firefox, IE) but that didn't solve it.

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 . 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.

Use the this keyword in the receiveXML_SOAPData function to reference the XHR object instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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