我在发出ajax呼叫时获得了404

时间:2014-09-24 19:24:03

标签: c# jquery ajax asp.net-web-api

这是我的ajax电话

$.ajax({
    async: false,
      url: "/api/clients/UpdateResourceContactProductsByResourceContactId/" + id,
     type: 'POST',
     data: { strIds: strIds },
  success: function (data) {
   }
});

其中id是整数,strIds是整数的字符串contantenation,它们看起来像 123_254_741_6650 ...

这是服务器端代码......

[HttpPost]
public IHttpActionResult UpdateResourceContactProductsByResourceContactId
       (int id, string strIds)
{
   //...
}

当我点击更新按钮时,我收到以下错误:

{"Message":"No HTTP resource was found that matches the request URI 
 'http://localhost/api/clients/UpdateResourceContactProductsByResourceContactId/22757'.",
 "MessageDetail":"No action was found on the controller 'Clients' that matches the request."}

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

试试这个......

$.ajax({
    async: false,
    url: "/api/clients/UpdateResourceContactProductsByResourceContactId",
    type: 'POST',
    data: { id: id, strIds: strIds },
    success: function (data) {
   }
 });

答案 1 :(得分:0)

我认为你传递的数据错了。你正在传递一个物体。更改方法以接受JObject并使用dynamic拉出strIds,单独传递字符串,或将其用作URL参数。

//example using JObject below
[HttpPost]
public IHttpActionResult UpdateResourceContactProductsByResourceContactId(int id, JObject data)//JObject requires Json.NET
{
   dynamic json = data;
   string ids = json.strIds
}

如果你要POST一个对象,你需要在javascript中调用JSON.stringify。

var data = JSON.stringify({ strIds: strIds });
$.ajax({
    async: false,
      url: "/api/clients/UpdateResourceContactProductsByResourceContactId/" + id,
     type: 'POST',
     data: data,
  success: function (data) {
   }
});