jQuery的WCF SOAP调用失败jQuery、WCF、SOAP

2023-09-04 02:52:37 作者:温柔又嚣张

任何人都知道我可以做一个jQuery的SOAP调用到WCF服务?

我的JQuery的code:

  $。阿贾克斯({
    网址:HTTP://localhost/oseop/orderingservice.svc/HelloWorld
    数据:$(#txtTestRequest)VAL()。
    键入:POST,
    过程数据:真正的,
    的contentType:应用程序/ XML;字符集= UTF-8,
    超时:10000,
    数据类型:XML,
    beforeSend:功能(XHR){
        xhr.setRequestHeader(SOAPAction的,HelloWorld的);
    },
    成功:函数(XML){
        的console.log(Sucess);
        执行console.log(XML);
    },
    错误:函数(XHR){
        执行console.log(xhr.statusText);
    }
});
 

我在txtTextRequest数据:

 <肥皂:信封XMLNS:肥皂=htt​​p://schemas.xmlsoap.org/soap/envelope/的xmlns:XSI =HTTP://www.w3。组织/ 2001 / XML模式实例的xmlns:XSD =http://www.w3.org/2001/XMLSchema>
  < S:头的xmlns:S =htt​​p://schemas.xmlsoap.org/soap/envelope/>
    &LT;为s:mustUnderstand属性=1 xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:49720/OrderingServices/OrderingService.svc</To>
    &lt;作用S:mustUnderstand属性=1 xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://temp.org/test/IOrder/HelloWorld</Action>
  &LT; / S:头&GT;
  &LT;肥皂:身体与GT;
    &LT;的HelloWorld的xmlns =htt​​p://temp.org/test/&GT;
      &LT;名称&gt;我的名字是伯爵&LT; /名称&gt;
    &LT; / HelloWorld的&GT;
  &LT; / SOAP:身体与GT;
&LT; /肥皂:信封&GT;
 

我的C#code:

  [的ServiceContract(命名空间=htt​​p://temp.org/test/)
公共接口IOrder
{
    [OperationContract的]
    [WebInvoke(方法=POST
                 BodyStyle = WebMessageBodyStyle.Wrapped,
                 ResponseFormat = WebMessageFormat.Xml,
                 RequestFormat = WebMessageFormat.Xml)

    字符串的HelloWorld(字符串名称);

}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)
公共类OrderingService:IOrder
{
    公共字符串的HelloWorld(字符串名称)
    {
        返回的String.Format(你好{0},名称);
    }

}
 
使用 VS WCF 调用P6 SOAP Web Service

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

  

未捕获的异常:[异常...   组件返回故障code:   0x80040111(NS_ERROR_NOT_AVAILABLE)   [nsIXMLHtt prequest.statusText]   nsresult:0x80040111   (NS_ERROR_NOT_AVAILABLE)的位置:   JS框架::   的http://本地主机:49758 / TestClient的/ JS /的script.js   ::匿名:: 22行的数据:无]

     

行0

编辑#1:

我的请求报头:

 选项HTTP://localhost/oseop/orderingservice.svc/HelloWorld HTTP / 1.1
主持人:本地主机
用户代理:Mozilla的/ 5.0(Windows系统; U; Windows NT的6.1; EN-US; rv中:1.9.2.13)的Gecko / 20101203 Firefox的/ 3.6.13
接受:text / html的,是application / xhtml + xml的,应用程序/ XML; Q = 0.9,* / *; Q = 0.8
接受语言:EN-US,EN; Q = 0.5
接受编码:gzip,紧缩
接收字符集:ISO-8859-1,UTF-8,Q = 0.7,*; Q = 0.7
保持活动:115
连接:保持活动
原产地:HTTP://本地主机:49758
访问控制请求-方法:POST
访问控制请求报头:SOAPAction报
 

响应:

  HTTP / 1.1 400错误的请求
服务器:Microsoft-IIS / 7.5
的X已启动方式:ASP.NET
日期:周五,2011 14点00分53秒格林尼治标准​​时间07一月
内容长度:0
 

解决方案

看看的 jQuery的。阿贾克斯()的API文档。它说:

错误(jqXHR,textStatus,errorThrown)

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

您的错误可能是被扔在试图访问状态文本,它可能是没有一个可用。尝试添加其他两个参数到code和检查的内容。

此外,该浏览器您使用?有在XmlHtt prequests被各种浏览器中的处理方式的一些行为差异,所以它试图在一个以上有时可以帮助诊断这些事

Anyone know i can make a jquery soap call to a wcf service?

My JQuery Code:

$.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);
    }
});

My data in 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>

My C# code:

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

}

I get the following error from xhr.statusText:

uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.statusText]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost:49758/TestClient/js/script.js :: anonymous :: line 22" data: no]

Line 0

Edit #1:

My Request headers:

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

Response:

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

解决方案

Take a look at the jQuery.ajax() API documentation. It says:

error(jqXHR, textStatus, errorThrown)

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

Your error is probably being thrown by the attempt to access statusText, it's possible that there isn't one available. Try adding the other two arguments to your code and checking their contents.

Also, which browser are you using? There are some behavioral differences in the way XmlHttpRequests are handled among the various browsers, so trying it in more than one can sometimes help in diagnosing these things.