【问题标题】:NetworkError: 405 Method Not Allowed in WCF REST service in Firefox browserNetworkError:Firefox 浏览器中的 WCF REST 服务中不允许使用 405 方法
【发布时间】:2013-02-18 10:49:03
【问题描述】:

可能重复: NetworkError: 405 Method Not Allowed in WCF

我在 jQuery AJAX POST 中调用了一个 REST 服务。它在 IE8 中运行良好。但在 Firefox 中显示“NetworkError: 405 Method Not Allowed”。我搜索了很多网站,但无法得到明确的答案。

我的界面代码:

[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);
}   
});
}

服务

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;
}      

}

提前谢谢..

**EDIT** <br/>

来自博客http://blog.weareon.net/calling-wcf-rest-service-from-jquery-causes-405-method-not-allowed/ 我添加了以下代码。但它绕过了 "if" 条件。

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();
}           
}

【问题讨论】:

  • 您能检查一下 firefox 正在向您的 wcf 服务发送什么 http 动词吗?
  • @Obaid 如何查看http动词??
  • 在 Firefox 中使用 Web 控制台 (Ctrl + Shift + K)。查看使用什么 http 动词来调用您的网络服务(POST 或 OPTION)
  • @Obaid 。它的 OPTIONS http 动词。请检查我的编辑。

标签: c# asp.net rest jquery firefox


【解决方案1】:

这就是答案 从这个link

在服务应用程序中添加了以下代码。在 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();
}           
}

【讨论】:

    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2011-01-13
    • 2012-04-21
    • 2017-11-18
    • 2017-10-10
    • 2016-09-23
    相关资源
    最近更新 更多