0

可能重复: 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/我添加了以下代码。但它绕过了“如果”条件。

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

1 回答 1

2

这是来自此链接的答案

在服务应用程序中添加了以下代码。在 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();
}           
}
于 2013-02-18T12:09:43.243 回答