除了IE之外,从JavaScript调用Web服务无法正常工作

时间:2011-12-12 06:01:08

标签: javascript ajax web-services

我正在调用位于其他主机上的Web服务。 它在IE中运行良好,但不适用于FF,Opera等。 这是我的代码:

    if(xmlHttpReq.readyState == 0){
        xmlHttpReq.open('POST', strURL, true);
        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
                var resultString = xmlHttpReq.responseXML;
                document.getElementById('webserviceresponsetext').value = resultString.text;
            }
        }
        xmlHttpReq.send(packet);
    }
}

var packet = '<?xml version="1.0" encoding="utf-8" ?>' +  
'<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/"> '+
'<soap:Body>'+
'<authenticate xmlns="http://interfaces.service.webservices.webs.eic.com">" '+
'<ParameterName>abc</ParameterName>'+
'<ParameterName>1234</ParameterName>'+
'</authenticate></soap:Body>'+
'</soap:Envelope>';

此方法只调用authenticate方法,如果用户abc是有效用户,则返回true / false.1234是abc用户的密码。 请帮忙... 提前谢谢......

我在FF中收到此错误:

XML Parsing Error: no element found Location: moz-nullprincipal:{cb5142f9-33a8-44ca-bc9d-60305ef7cea8} Line Number 1, Column 1:

1 个答案:

答案 0 :(得分:1)

如果您希望ajax可以跨浏览器可靠地运行,我建议您使用jQuery

它将直接消除处理XmlHttpReq的复杂性,为您提供更清晰的语法,并在所有主流浏览器中工作。

首先查看jQuery's ajax API

您的代码可能与此类似:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
  $(this).addClass("done");
 }
});
相关问题