我有C#編寫的一臺服務器,並且嘗試使用jQuery來發送一個DELETE請求其他服務器上的網頁上的WebAPI:JQuery的DELETE請求到C#API - > HTTP狀態碼405
<script>
$(function() {
$("#Save").click(function() {
$.ajax({
type: "DELETE",
url: "https://chad-test4.clas.uconn.edu/api/Employees/1",
dataType: "json",
success: function (data) {
console.log("Response recieved");
console.log("Success: " + JSON.stringify(data));
},
error: function() {
console.log("Failed")
}
// data: {"id": 1 }
});
});
});
</script>
這是我的控制器:
// DELETE: api/Employees/5
//[ResponseType(typeof(JsonObjectContract))]
public HttpResponseMessage Deleteemployee(int id)
{
employee employee = db.employees.Find(id);
var jsonid = employee.id;
if (jsonid == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error: Employee does not exist.");
}
db.employees.Remove(employee);
db.SaveChanges();
var Success = this.Request.CreateResponse(HttpStatusCode.OK, "Success");
Success.Content = new StringContent("{ Success: Success }", Encoding.UTF8, "application/json");
return Success;
}
我收到的控制檯的響應是:
XMLHttpRequest cannot load https://chad-test4.clas.uconn.edu/api/Employees/1.
Invalid HTTP status code 405
create.htm:41 Failed
在上面做研究後IC,我改變了我的web.config頭建議的文字:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
<remove name="FormsAuthentication" />
</modules>
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
我仍然得到同樣的反應。任何建議表示讚賞
請包括您所指的代碼。 – Mike