jquery - 使用 basichttpbinding 调用 wcf webservice,无需 REST 或 JSON

标签 jquery ajax wcf basichttpbinding

我有一个通过 wsHTTPBinding 和 basicHTTPBinding 公开的 wcf Web 服务。后者将其端点地址指定为“/basic”,如下所示:

    <endpoint address="/basic" binding="basicHttpBinding" bindingConfiguration="theAwesomeBasicHttpServerBinding" contract="LOServices.Proxy.ServiceContracts.ILOService">
      <identity>
        <servicePrincipalName value="HTTP/MY_MACHINE_NAME" />
      </identity>
    </endpoint>

绑定(bind)定义如下:

  <basicHttpBinding>
    <binding name="theAwesomeBasicHttpServerBinding" maxReceivedMessageSize="5000000">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>

我正在尝试使用 jquery 调用其中一个 webmethods。因为这是基于 basicHTTPBinding 而不是 RESTful,因此,我不会使用 JSON。因此,我通过 wcf 测试客户端对服务进行的先前(成功)调用构建了请求 XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ILOService/GetLoanActions</Action>
  </s:Header>
  <s:Body>
    <GetLoanActions xmlns="http://tempuri.org/">
      <request xmlns:d4p1="http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <d4p1:AssociationNumber>3</d4p1:AssociationNumber>
        <d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings>
      </request>
    </GetLoanActions>
  </s:Body>
</s:Envelope>

因此,我使用的实际 JavaScript 的结构如下:

        function callWs() {
            var theRequest = " \
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
  <s:Header> \
    <Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/ILOService/GetLoanActions</Action> \
  </s:Header> \
  <s:Body> \
    <GetLoanActions xmlns=\"http://tempuri.org/\"> \
      <request xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/LOServices.Proxy.Requests\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"> \
        <d4p1:AssociationNumber>3</d4p1:AssociationNumber> \
        <d4p1:IgnoreWarnings>false</d4p1:IgnoreWarnings> \
      </request> \
    </GetLoanActions> \
  </s:Body> \
</s:Envelope>";

            $.ajax({
                type: "POST",
                url: "http://localhost/LOServices/Services/LOService.svc/basic",
                data: theRequest,
                timeout: 10000,
                contentType: "text/xml",
                dataType: "xml",
                async: false
            });
        }

然而,当我以这种方式运行 wcf 服务时,我很不幸地收到了来自 wcf 服务的“错误请求”,这并没有给我留下太多可以跟进的信息。

我已经努力尝试完成这项工作至少 3 个小时了,所以如果有人有想法,请随时提出一些建议。

谢谢。

最佳答案

对于 BasicHttpBinding 请求,您需要在 HTTP 请求中设置 SOAPAction header (这将定义哪个操作应接收消息)。

$.ajax({
      type: "POST",
      url: "http://localhost/LOServices/Services/LOService.svc/basic",
      data: theRequest,
      timeout: 10000,
      contentType: "text/xml",
      dataType: "xml",
      async: false,
      headers: { "SOAPAction": "http://tempuri.org/Contract/Operation" }
    });

请注意,headers 选项是 jQuery 1.5 中的新选项,因此如果您使用的是较旧的选项,则需要使用 beforeSend 函数。

此外,您将来应该能够通过 enabling tracing 获得有关“错误请求”或“内部服务器错误”等错误的更多信息。对于 WCF - 跟踪将包含有关问题的更多详细信息。

关于jquery - 使用 basichttpbinding 调用 wcf webservice,无需 REST 或 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6712842/

相关文章:

ajax - `success: function(msg)` 在我的 jQuery Ajax 调用中意味着什么?

asp.net-mvc - 为 IIS 托管的 .SVC 文件配置 XML-RPC 行为?

javascript - 响应来自 python Web 应用程序的 ajax 请求

javascript - 如何在使用数据表发送到模板之前获得 AJAX 响应?

javascript - 在多个页面上使用多个 jQuery 插件?

javascript - 从 API MVC C# 填充下拉选项值

c# - 错误 : Visual Studio 2022 "The build must be stopped before the solution can be closed." Disable Bullid option after build

c# - WCF 服务上的自定义基本身份验证

php - 将调用的 PHP 页面返回的值 append 到表中

javascript - 无法更改按钮颜色