2013-02-18 126 views
0

可能複製: NetworkError: 405 Method Not Allowed in WCFNetworkError:405不允許的方法在WCF REST服務在Firefox瀏覽器

我打電話jQuery的AJAX POST REST服務。它在IE8中工作正常。但在Firefox中顯示「NetworkError:405方法不允許」。我搜索了很多網站,但無法得到明確的答案。

接口代碼:

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

你可以檢查http動詞firefox發送到你的wcf服務嗎? – Obaid 2013-02-18 10:58:16

+0

@Obaid如何檢查http動詞? – kk1076 2013-02-18 11:09:59

+0

在Firefox中使用Web控制檯(Ctrl + Shift + K)。查看使用http動詞調用您的web服務(POST或OPTION) – Obaid 2013-02-18 11:18:04

回答

2

下面是答案 從這個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(); 
}   
} 
相關問題