如何使用javascript来使用轴Web服务?

时间:2011-03-15 18:19:20

标签: jquery web-services

我是轴新手。我想使用jquery消耗一个轴webserive。我设置了一个简单的轴webservice(add方法)并使用以下代码向它发送一条soap消息:

    var xmlMsg = "<soapenv:Envelope xmlns:q0=\"http://add.example.ws\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><q0:add><q0:a1>3.5</q0:a1><q0:a2>5.8</q0:a2></q0:add></soapenv:Body></soapenv:Envelope>"
    //var xmlMsg = "<?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><ns1:add xmlns:ns1=\"urn:xmethods-add\"><a1>2.3</a1><a2>4.5</a2></ns1:add></soap:Body></soap:Envelope>";
    alert(xmlMsg);
    $.ajax({ 
        type: "POST", 
        contentType:"text/xml; charset=\"utf-8\"", 
        url:"http://localhost:8080/webServiceProj/services/AddService",  //http://localhost:8080/webServiceProj/services/AddService //http://127.0.0.1:8080/soap-proxy/soap/add/1.1 
        data:xmlMsg, 
        dataType:'xml',
        success: function(xml) { 
            if($.browser.msie){ 
                $("#result").append(xml.getElementsByTagName("ns1:out")[0].childNodes[0].nodeValue+"<br/>"); 
            } 
            else{ 
                $(xml).find("out").each(function(){ 
                    $("#result").append($(this).text()+"<br/>"); 
                }) 
            } 
        }, 
        error: function(x, e) { 
            alert('error:'+x.responseText); 
        }, 
        complete: function(x) { 
            alert('complete:'+x.responseText); 
        } 
    }); 

但是我从响应中得到的是说SOAPAction错误是一个错误。谁能给我一些方向吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

这个jQuery的SOAP插件可能会有所帮助:

http://plugins.jquery.com/project/jqSOAPClient

答案 1 :(得分:0)

您必须将SOAPAction标头添加到请求中,例如您已添加的类型和内容类型。