使用javascript SOAP请求使用WCF服务
我有一个WCF服务,我必须使用JavaScript SOAP请求来使用它。JavaScript是Phonegap应用程序的一部分,因此我不从本地服务器而是从文件运行此JavaScript 当WCF服务运行localhost时,一切正常。但是,一旦我在服务器上部署WCF服务,就会出现错误(服务的名称及其方法仅供说明): 我知道这是一个跨域错误,但这就是为什么在我的Web服务的global.asax中添加以下方法的原因。但那没用使用javascript SOAP请求使用WCF服务,javascript,wcf,cordova,soap,Javascript,Wcf,Cordova,Soap,我有一个WCF服务,我必须使用JavaScript SOAP请求来使用它。JavaScript是Phonegap应用程序的一部分,因此我不从本地服务器而是从文件运行此JavaScript 当WCF服务运行localhost时,一切正常。但是,一旦我在服务器上部署WCF服务,就会出现错误(服务的名称及其方法仅供说明): 我知道这是一个跨域错误,但这就是为什么在我的Web服务的global.asax中添加以下方法的原因。但那没用 protected void Application_BeginReq
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept,SOAPAction");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
javascript调用:
$.support.cors = true;
var bodyrequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<s:Body>" +
"<"getAll xmlns=\"http://tempuri.org/\" />" +
"</s:Body>" +
"</s:Envelope>";
return $.ajax({
type: "POST",
//This works
//url: "http://localhost:49704/myWebservice/service.svc",
//this doesn't work
url: "http://myWebservice/service.svc",
data: bodyrequest,
timeout: 10000,
contentType: "text/xml",
dataType: "xml",
beforeSend: function (xhr) {
xhr.setRequestHeader("SOAPAction", "http://tempuri.org/service/getAll ");
},
});
$.support.cors=true;
var bodyrequest=“”+
"" +
“这看起来像个老问题。你已经解决了吗?
如果没有,你能把实际的请求发送到服务器吗?试着用Fiddler捕捉它
$.support.cors = true;
var bodyrequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<s:Body>" +
"<"getAll xmlns=\"http://tempuri.org/\" />" +
"</s:Body>" +
"</s:Envelope>";
return $.ajax({
type: "POST",
//This works
//url: "http://localhost:49704/myWebservice/service.svc",
//this doesn't work
url: "http://myWebservice/service.svc",
data: bodyrequest,
timeout: 10000,
contentType: "text/xml",
dataType: "xml",
beforeSend: function (xhr) {
xhr.setRequestHeader("SOAPAction", "http://tempuri.org/service/getAll ");
},
});
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<services>
<service name="myWebservice/service">
<endpoint address="" binding="basicHttpBinding" contract="Contract.myWebservice"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<appSettings>
...
</appSettings>