如何在AJAX调用中使用SOAP Request

时间:2011-12-15 10:10:15

标签: ajax soap

这是我的AJAX应用程序,我需要联系我在服务器上运行的Web服务。

function sendRequest(method, url)
{
method == 'post';
{
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}

这是我从SOAP UI中获取的SOAP请求,它正常工作

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.bayer.tata.com/" xmlns:tkw="http://tata.com/bayer" xmlns:chim="http://tata.com/chimera">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:strategy>
         <!--Optional:-->
         <request>
           <xmlMessage>
<![CDATA[<test>or like this</test>]]>
</xmlMessage>
        </request>
      </ser:strategy>
   </soapenv:Body>
</soapenv:Envelope>

请告诉我如何在sendRequest函数中使用此SOAP XML消息。 我只使用普通的Java Script AJAX(没有像Jquery,DOJO或其他任何东西)

1 个答案:

答案 0 :(得分:0)

我认为this Post可以帮到你。但是,如果请求不需要SOAP标头或其他奇怪的东西,大多数Web服务器允许您使用普通的HTTP Post(在body请求中没有SOAP格式)来调用webservices。

.NET和普通javaScript中的一个例子:

.NET Web服务

<System.Web.Services.WebService(Namespace:="http://JuntaEx/Agricultura/SegurInfo/GestorFirmaExterno/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class GestorFirmaExterno
    Inherits System.Web.Services.WebService

<WebMethod(Description:="Retorna los documentos originales asociados a un identificador de firma pasado como parámetro.")> _
    Public Function ObtenerDocumentoOriginal(ByVal idFirma As String) As DocumentoED()
//code
    End Function
End Class

的web.config:

 <webServices>
    <protocols>
        <add name="HttpSoap"/>
        <add name="HttpPost"/> <!-- Allows plain HTTP Post -->
        <add name="HttpSoap12"/>
        <!-- Documentation enables the documentation/test pages -->
        <add name="Documentation"/>
     </protocols>
 </webServices>

JavaScript请求:

function crearRequest(url) {

    if (window.XMLHttpRequest) { 
        peticion_http = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { 
        peticion_http = new ActiveXObject('Microsoft.XMLHTTP');
    }
    peticion_http.open('POST', url, true); //sync
    peticion_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    return peticion_http;
}

    peticion_http = crearRequest('http://localhost/wspuenteFirma/serviciopuente.asmx/ObtenerDocumentoOriginal');
    peticion_http.onreadystatechange = obtenerDocHandler;
    var query_string = 'IdFirma=' + encodeURIComponent(docId);
    peticion_http.setRequestHeader('Content-Length', query_string.length);
    peticion_http.send(query_string);

您将此请求发送到服务器:

POST /wsGestorFirmaExterno/GestorFirmaExterno.asmx/ObtenerDocumentoOriginal HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

idFirma=string

并从服务器回复此响应:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDocumentoED xmlns="http://JuntaEx/Agricultura/SegurInfo/GestorFirmaExterno/">
  <DocumentoED>
    <hash>string</hash>
  </DocumentoED>
  <DocumentoED>
    <hash>string</hash>
  </DocumentoED>
</ArrayOfDocumentoED>

使用javascript解析它以获取您需要的信息。

PS:您可以配置服务器和浏览器请求来发送和接收JSON数据而不是XML。

我希望它有所帮助。