我想使用来自clientside的AJAX将JSON对象传递给WCF服务。在Internet Explorer中一切正常,但在Firefox中没有。
在Firefox中我得到 405:方法不允许
这是我将json数据(从客户端脚本)传递到WCF服务...
$(document).ready(function () {
var Author = '{ "Id": "A01", "Name": "Ravinder" }';
$.ajax({
type: "POST",
data: JSON.stringify(Author),
contentType: "application/json; charset=utf-8",
datatype: "json",
url: "http://localhost:53905/Service1.svc/AuthorPostByJson",
success: function (data) {
alert("success");
},
error: function (xmlhttprequest, textstatus, errorthrown) {
alert(" failed ");
console.log("error: " + errorthrown);
}
});//end of $.ajax
});
我的WCF服务就像......
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "AuthorPostByJson",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<Book> GetBooksByAuthor_JSON(Author author);
我的web.config文件......
<system.serviceModel>
<services>
<service behaviorConfiguration="Platform.WebRestful.Service1Behavior"
name="Platform.WebRestful.Service1">
<endpoint address="" binding="basicHttpBinding" contract="Platform.WebRestful.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="Platform.WebRestful.BookServiceHostRestfulBehavior"
name="Platform.WebRestful.BookServiceHostRestful">
<endpoint address="" binding="webHttpBinding" contract="Platform.WebRestful.IBookServiceHostRestful">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Platform.WebRestful.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="Platform.WebRestful.BookServiceHostRestfulBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
答案 0 :(得分:1)
最后我在一些文章中找到了答案。他们说跨越任何跨域(站点)HTTP请求,第一个浏览器将发送称为“预检请求”的“OPTIONS”请求... “preflighted”请求首先向另一个域上的资源发送HTTP OPTIONS请求标头,以确定实际请求是否可以安全发送,并且此请求需要适当的标头,说服务允许访问 该服务作为回应
为实现这一目标,我们有两个解决方案...... 1)WCF自定义行为 2)修改Global.asax文件的Application_BeginRequest事件。
我跟着第二个...... 解决方案是将 Global.asax 文件添加到WCf服务项目并添加
following code in that,then it perfectly works across any browser...
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");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}