繁体   English   中英

NetworkError:Firefox浏览器中的WCF REST服务中不允许405方法

[英]NetworkError: 405 Method Not Allowed in WCF REST service in Firefox browser

可能重复: NetworkError:WCF中不允许405方法

我在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中运行良好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM