【发布时间】:2013-09-23 08:04:00
【问题描述】:
我正在尝试从 JQuery 调用 C# WCF SOAP Web 服务。
服务托管在 IIS 端口 80 上,客户端从 Apache 端口 81 运行,两者都来自 localhost。所以我陷入了跨域请求。
使用 Chrome,我得到了预期的结果,但是查看网络流量,它显示
OPTIONS请求返回错误400 Bad Request,但是下一个POST请求成功:使用 Firefox,出现错误(JavaScript 警报显示
null | error |)。似乎POST请求未发送,因为OPTIONS请求失败:使用 IE,一切正常,就好像它来自同一个来源......我没有看到任何
OPTIONS请求:
从接口暴露的功能:
namespace Test
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
String HelloWorld();
}
}
服务配置(Web.config):
<services>
<service name="Test.MathService" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:80/WCFtest/service.svc" />
</baseAddresses>
</host>
<endpoint address="/soap" binding="basicHttpBinding" contract="Test.IMathService" />
<endpoint address="/rest" binding="webHttpBinding" contract="Test.IMathService" behaviorConfiguration="WEB" />
</service>
</services>
[...]
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:81" />
<add name="Access-Control-Allow-Headers" value="SOAPAction, Content-type, Accept, Origin" />
<add name="Access-Control-Allow-Methods" value="POST, GET, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
JQuery 代码:
$('#btn_helloworld').click(function () {
var soapRequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<tem:HelloWorld/>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
$.ajax({
type: 'POST',
url: 'http://localhost/WCFtest/service.svc/soap',
crossDomain: true,
contentType: 'text/xml',
dataType: 'xml',
data: soapRequest,
beforeSend: function (xhr) {
xhr.setRequestHeader('SOAPAction', 'http://tempuri.org/IMathService/HelloWorld');
},
success: function (data) {
$('#outputHelloWorld').val($(data).find('HelloWorldResponse').text());
},
error: function (jqxhr, textStatus, errorThrown) {
alert(jqxhr.responseXML + ' | ' + textStatus + ' | ' + errorThrown);
}
});
});
如果从同一来源调用,服务工作正常。
我错过了什么吗?
【问题讨论】:
-
你有没有得到这个工作?
-
不,但如果我必须回到问题上并找到解决方案,我肯定会更新我的帖子。
标签: jquery wcf web-services soap cross-domain