我正在尝试使用jQuery从ajax调用中调用WCF服务。 我设法从SOAP-UI和Excel/VBA中调用WCF。 我的问题来自发送的OPTIONS请求,但没有POST请求:
- 如果我将URL设置为
http://mywcf/service.svc
,则会发送OPTIONS并获得400 Bad Request
状态,并且不会发送POST请求。在这种情况下,标头中缺少HTTP/1.1
(与SOAP-UI标头相比)。 - 如果我将URL设置为
http://mywcf/service.svc HTTP/1.1
,则会发送OPTIONS并获得200 OK
状态,但不会发送POST请求。在这种情况下,HTTP/1.1
似乎被解释为文件名。
有人能告诉我如何从javascript调用WCF的POST操作并添加HTTP/1.1
标头,而不破坏服务URL吗?
这是我ajax调用的摘录:
var soapData = ''
+'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mic="http://microsoft.wcf.documentation">'
+' <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsrm="http://docs.oasis-open.org/ws-rx/wsrm/200702">'
+' <wsrm:Sequence>'
+' <wsrm:Identifier>s:Sender a:ActionNotSupported</wsrm:Identifier>'
+' <wsrm:MessageNumber>1</wsrm:MessageNumber>'
+' </wsrm:Sequence>'
+' <wsa:Action>http://schemas.xmlsoap.org/ws/2005/02/rm/CreateSequence</wsa:Action>'
+' <wsa:ReplyTo>'
+' <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>'
+' </wsa:ReplyTo>'
+' <wsa:MessageID>uuid:'+ MsgUid +'</wsa:MessageID>'
+' <wsa:To>'+ Url +'</wsa:To>'
+' </soap:Header>'
+' <soap:Body xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm">'
+' <wsrm:CreateSequence>'
+' <wsrm:AcksTo xmlns:wsa="http://www.w3.org/2005/08/addressing">'
+' <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>'
+' </wsrm:AcksTo>'
+' <wsrm:Offer>'
+' <wsrm:Identifier>urn:soapui:'+ SeqUid +'</wsrm:Identifier>'
+' </wsrm:Offer>'
+' </wsrm:CreateSequence>'
+' </soap:Body>'
+'</soap:Envelope>';
$.ajax({
type: 'POST',
url: 'http://mywcf/service.svc', // with or without +' HTTP/1.1'
data: soapData,
contentType: 'application/soap+xml;charset=UTF-8',
dataType: 'xml'
});
我 WCF 的 web.config
中的值:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>