[英]NetworkError: 405 Method Not Allowed in WCF REST service in Firefox browser
Possibly Duplicate : NetworkError: 405 Method Not Allowed in WCF 可能重复: NetworkError:WCF中不允许405方法
I called a REST service in jQuery AJAX POST. 我在jQuery AJAX POST中调用了REST服务。 It works fine in IE8.
它在IE8中工作正常。 But shows " NetworkError: 405 Method Not Allowed " in Firefox .
但在Firefox中显示“ NetworkError:405 Method Not Allowed ”。 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 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. 来自博客http://blog.weareon.net/calling-wcf-rest-service-from-jquery-causes-405-method-not-allowed/我添加了以下代码。 But it bypasses the "if" condition.
但它绕过了“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();
}
}
Here is the answer From this link 这是答案来自这个链接
Added the below code in service application. 在服务应用程序中添加了以下代码。 Works well in Firefox.
在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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.