WCF サービスがあり、JavaScript SOAP 要求を使用してそれを使用する必要があります。JavaScript は Phonegap アプリケーションの一部であるため、この JavaScript はローカル サーバーからではなく、ファイルから実行します。
WCF サービスが localhost を実行している場合、すべて正常に動作します。しかし、WCF サービスをサーバーにデプロイするとすぐに、エラーが発生します (サービスの名前とそのメソッドは説明のためのものです)。
OPTIONS http://myWebservice/service.svc 400 (Bad Request)
XMLHttpRequest cannot load http://myWebservice/service.svc. Origin null is not allowed by Access-Control-Allow-Origin.
これがクロスドメイン エラーであることはわかっていますが、そのため、Web サービスの global.asax に次のメソッドを追加しました。しかし、それは役に立ちません。
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 ");
},
});
wcf サービスの web.config:
<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>
誰にもアイデアはありますか?または、WCF サービスへのクロスドメイン SOAP Javascript 要求を行う別の方法はありますか?