use*_*905 17 asp.net-mvc asp.net-web-api
我在API控制器上有以下内容:
public void UpdateClient(Client client)
{
try
{
if (ModelState.IsValid)
{
db.Entry(client).State = EntityState.Modified;
db.SaveChanges();
}
}
catch
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
}
Run Code Online (Sandbox Code Playgroud)
以及页面上的以下内容:
$.ajax({
url: "api/client/UpdateClient",
type: "PUT",
contentType: 'json',
data: ko.toJSON(model.selectedClient()),
success: function (result) {
getClients();
$("#loader").hide();
},
failure: function (result) {
alert(result.d);
$("#loader").hide();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("An error occurred, please try again.");
$("#loader").hide();
}
});
Run Code Online (Sandbox Code Playgroud)
但这会给错误405方法不允许,任何人都可以看到我可能出错的地方?作为参考,api的url是可以的,因为我也使用相同的api控制器来执行其他功能.
selectedClient()也是通过WebApi接收的Client对象,因此应该与PUT完全匹配.
Sal*_*man 23
如果您使用的是IIS7并安装了WebDav,请尝试将其删除.我只用PUT动词得到了同样的错误,它解决了这个问题
更新:您可以在此处阅读WebDav:http://www.iis.net/learn/get-started/whats-new-in-iis-7/what39s-new-for-webdav-and-iis-7
您的UpdateClient操作是否有[HttpPut]属性?另外,你有一条路线接受{action}作为routeTemplate吗?例如:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
还有一件事,在'ajax代码中使用'application/json'作为内容类型而不是'json'.
看起来这两行是错误的,我修改如下:
contentType: 'application/json',
data: "{client: " + ko.toJSON(model.selectedClient()) + "}",
Run Code Online (Sandbox Code Playgroud)
现在进去了。
| 归档时间: |
|
| 查看次数: |
31930 次 |
| 最近记录: |