本文介绍了NetworkError:405法在WCF REST服务不允许在火狐浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能复制:
NetworkError: 405法在WCF不允许

我打电话jQuery的AJAX POST REST服务。它在IE8工作正常。但显示 NetworkError:405不允许的方法中的火狐。我搜索了很多网站,但不可能得到一个明确的答案。

我的接口 code:

  [的ServiceContract]
公共接口IEShop
{
 [OperationContract的]
 [WebInvoke(方法=POST,UriTemplate =/ InsertDetails,BodyStyle = WebMessageBodyStyle.Bare,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)
 字符串InsertDetails(EShopInputParameters EcomInput);
}

jQuery的

  VAR postCall =功能(){
VAR输入=
{
用户名:123456,
PARTNERID:CSWP
};
警报(JSON.stringify(输入));
$阿贾克斯({
键入:POST,
网址:HTTP://本地主机:36196 / Service.svc / InsertDetails
数据:JSON.stringify(输入),
的contentType:应用/ JSON
数据类型:JSON
成功:函数(响应){
警报(响应);
},
错误:功能(XHR,状态,错误){
警报(错误);
}
});
}

服务

 公共字符串InsertDetails(EShopInputParameters EShopInput)
{
尝试
{
命令= database.GetStoredProcCommand(Data_Template.UspGetInsertDetails);
database.AddInParameter(指挥,Data_Template.UserID,DbType.String,EShopInput.Userid);
database.AddInParameter(指挥,Data_Template.PartnerID,DbType.String,EShopInput.partnerid);
database.ExecuteNonQuery(命令);
返回0;
}
赶上(例外错误)
{
扔犯错;
}

}

在此先感谢..

  **编辑**< BR />

从博客的我加了下面code。但它绕过了如果的条件。

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
HttpContext.Current.Response.AddHeader(访问控制允许原产地,*);
如果(HttpContext.Current.Request.HttpMethod ==选项。)
{
HttpContext.Current.Response.AddHeader(缓存控制,无缓存);
HttpContext.Current.Response.AddHeader(访问控制允许的方法,GET,POST);
HttpContext.Current.Response.AddHeader(访问控制允许报头,内容类型,接受,X-要求,以);
HttpContext.Current.Response.AddHeader(访问控制-max-age的,1728000);
HttpContext.Current.Response.End();
}
}


解决方案

下面就是答案
从这个链接

在添加服务应用程序下面的code。在Firefox效果很好。

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
HttpContext.Current.Response.AddHeader(访问控制允许原产地,*);
如果(HttpContext.Current.Request.HttpMethod ==选项。)
{
HttpContext.Current.Response.AddHeader(缓存控制,无缓存);
HttpContext.Current.Response.AddHeader(访问控制允许的方法,GET,POST);
HttpContext.Current.Response.AddHeader(访问控制允许报头,内容类型,接受,X-要求,以);
HttpContext.Current.Response.AddHeader(访问控制-max-age的,1728000);
HttpContext.Current.Response.End();
}
}

Possibly Duplicate : NetworkError: 405 Method Not Allowed in WCF

I called a REST service in jQuery AJAX POST. It works fine in IE8. But shows "NetworkError: 405 Method Not Allowed" in Firefox. I searched lot of websites but couldnt get a clear answer.

My Interface code :

[ServiceContract]
public interface IEShop
{
 [OperationContract]
 [WebInvoke(Method = "POST", UriTemplate = "/InsertDetails", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
 string InsertDetails(EShopInputParameters EcomInput);
}

Jquery

var postCall = function () {  
var input =
{   
Userid: "123456",
partnerid: "cswp"   
};
alert(JSON.stringify(input));                            
$.ajax({                
type: "POST",
url: "http://localhost:36196/Service.svc/InsertDetails",
data: JSON.stringify(input),
contentType: "application/json",
dataType: "json",
success: function (response) {                        
alert(response);
},
error: function (xhr, status, error) {
alert(error);
}   
});
}

Service

public string InsertDetails(EShopInputParameters EShopInput)
{
try
{
command = database.GetStoredProcCommand(Data_Template.UspGetInsertDetails);
database.AddInParameter(command, Data_Template.UserID, DbType.String, EShopInput.Userid);
database.AddInParameter(command, Data_Template.PartnerID, DbType.String, EShopInput.partnerid);
database.ExecuteNonQuery(command);
return "0";
}
catch (Exception err)
{
throw err;
}      

}

Thanks in advance..

**EDIT** <br/>

From the blog http://blog.weareon.net/calling-wcf-rest-service-from-jquery-causes-405-method-not-allowed/ I added the below code. But it bypasses the "if" condition.

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, x-requested-with");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}           
}
解决方案

Here is the answer From this link

Added the below code in service application. Works well in Firefox.

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, x-requested-with");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}           
}

这篇关于NetworkError:405法在WCF REST服务不允许在火狐浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:52