【问题标题】:jquery wcf soap call failjquery wcf 肥皂调用失败
【发布时间】:2011-01-06 18:57:27
【问题描述】:

有人知道我可以对 wcf 服务进行 jquery soap 调用吗?

我的 JQuery 代码:

$.ajax({
    url: "http://localhost/oseop/orderingservice.svc/HelloWorld",
    data: $("#txtTestRequest").val(),
    type: "POST",
    processData: true,
    contentType: "application/xml; charset=utf-8",
    timeout: 10000,
    dataType: "xml",
    beforeSend: function (xhr) {
        xhr.setRequestHeader("SOAPAction", "HelloWorld");
    },
    success: function (xml) {
        console.log("Sucess");
        console.log(xml);
    },
    error: function (xhr) {
        console.log(xhr.statusText);
    }
});

我在txtTextRequest中的数据:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:49720/OrderingServices/OrderingService.svc</To>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://temp.org/test/IOrder/HelloWorld</Action>
  </s:Header>
  <soap:Body>
    <HelloWorld xmlns="http://temp.org/test/">
      <name>my name is earl</name>
    </HelloWorld>
  </soap:Body>
</soap:Envelope>

我的 C# 代码:

[ServiceContract(Namespace = "http://temp.org/test/")]
public interface IOrder
{
    [OperationContract]
    [WebInvoke(Method = "POST",
                 BodyStyle = WebMessageBodyStyle.Wrapped,
                 ResponseFormat = WebMessageFormat.Xml,
                 RequestFormat = WebMessageFormat.Xml)]

    string HelloWorld(string name);

}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class OrderingService : IOrder
{
    public string HelloWorld(string name)
    {
        return String.Format("Hello {0}", name);
    }

}

我从 xhr.statusText 收到以下错误:

未捕获的异常:[异常... "组件返回故障码: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.statusText]" nsresult:“0x80040111 (NS_ERROR_NOT_AVAILABLE)”位置: "JS 框架:: http://localhost:49758/TestClient/js/script.js :: 匿名 :: line 22" 数据:无]

第 0 行

编辑#1:

我的请求标头:

OPTIONS http://localhost/oseop/orderingservice.svc/HelloWorld HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:49758
Access-Control-Request-Method: POST
Access-Control-Request-Headers: soapaction

回复:

HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Fri, 07 Jan 2011 14:00:53 GMT
Content-Length: 0

【问题讨论】:

  • 需要知道您在服务器上使用的 WCF 绑定和端点信息。我建议使用有效的 .net 客户端应用程序跟踪请求/回复,然后复制该有效负载和标头。
  • 添加了标题。这就是你要找的东西吗?

标签: javascript .net jquery wcf soap


【解决方案1】:

查看jQuery.ajax() API 文档。它说:

错误(jqXHR,textStatus,errorThrown)

请求失败时调用的函数。该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“timeout”、“error”、“abort”和“parsererror”。发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受一个函数数组。每个函数都会被依次调用。注意:跨域脚本和 JSONP 请求不会调用此处理程序。这是一个 Ajax 事件。

您的错误可能是由于尝试访问 statusText 而引发的,可能没有可用的。尝试将其他两个参数添加到您的代码中并检查它们的内容。

另外,您使用的是哪个浏览器?不同浏览器处理 XmlHttpRequests 的方式存在一些行为差异,因此在多个浏览器中尝试它有时可以帮助诊断这些问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2018-04-12
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多