如何在 AJAX 调用中使用 SOAP 请求

发布于 2024-12-21 08:51:32 字数 921 浏览 0 评论 0原文

这是我的 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 或任何)

This is my AJAX Application where i need to contact my Webservice running in server .

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

This is the SOAP Request which i picked up from the SOAP UI , which was working fine

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

Please tell me how can i use use this SOAP XML message within the sendRequest function .
I am using only plain Java Script AJAX ( Nothing like Jquery , DOJO , or any )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-28 08:51:32

我认为这篇文章可以帮助你。但是,如果请求不需要 SOAP 标头或其他奇怪的东西,大多数 Web 服务器允许您使用纯 HTTP Post(主体请求中没有 SOAP 格式)来调用 Web 服务。

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

我希望它有帮助。

I think this Post can help you. But most of web servers allows you to invoke webservices using plain HTTP Post (without SOAP format in the body request) if the request doesn't need SOAP headers or other weird things.

An example in .NET and plain javaScript:

.NET web service

<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 request:

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);

You send this request to the server:

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

idFirma=string

and recive this response from the server:

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>

Parse it with javascript to obtain the info you need.

PS: You can configure the server and the browser request to send and recive JSON data instead of XML.

I hope it's helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文