使用JavaScript使用SOAP Web服务

时间:2012-09-14 14:40:09

标签: javascript web-services soap

我正在尝试使用带有JavaScript的SOAP(.net)WebService,但responseText和responseXML为null。我尝试在另一个浏览器(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");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您尝试在ajax_request函数receiveXML_SOAPData中使用undefined。您应该从中获得异常,请检查您的错误控制台。

ajaxrequest函数中的sendDataAsXML_SOAP变量是a)未使用,b)该函数的本地变量 - 它不起作用。

使用this函数中的receiveXML_SOAPData关键字来引用XHR对象。