2017-02-10 10 views
0

から働いていませんこんにちは、私は、しかし、私はエラーを取得していますトラブルWEBAPIアクションメソッドへのAjaxのポストをやって過ごしています:はWEBAPI POST AJAX呼び出し

以下

{"Message":"No HTTP resource was found that matches the request URI ...","MessageDetail":"No action was found on the controller that matches the request."}

は私のAJAX呼び出しです:

$.ajax({ 
        url: "http://localhost/SomeService/api/ControllerName/TestPostMethod", 
        type: "POST", 
        data: JSON.stringify({ userId: "testuser', loc: "test" }), 
        contentType: "application/json;charset=utf-8", 
        success: function (msg) { 
         returnVal = msg; 
        }, 
        error: function (xhr, msg) { 
         var response = JSON.parse(xhr.responseText); 
         if (xhr.readyState == 4) { 
          if (xhr.status != 401) { 
           if (response.Message) { 
            console.log(response.Message); 
           } 
          } 
         } else { 
          if (response.Message) { 
           console.log(response.Message); 
          } 
         } 
        } 
       }); 

以下、私のWebAPIの方法である:

[System.Web.Http.HttpPost()] 
     public HttpResponseMessage TestPostMethod([FromUri]string userId, string loc) 
     { 
      return Request.CreateResponse<string>(HttpStatusCode.OK, "success"); 
     } 

これはルートが設定されている方法です。

  config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{action}/{id}", 
       defaults: new { id = RouteParameter.Optional }, 
       constraints:null 
      ); 
+0

は、ここに私の答えが見つかりました:をhttp://stackoverflow.com/questions/13473334/getting-no-action-was-found-from-webapi – desiguy

+0

'TestPostMethod'メソッドでモデルを定義してデータを送信する必要があります。 –

答えて

0

この問題を解決する最も簡単な方法(他の方法が存在してもよい)UserLocationようなカスタムオブジェクトを定義することであろう -

public class UserLocation 
{ 
    public string userId { get; set; } 
    public string loc { get; set; } 
} 

これを行った後、([FromBodyにTestPostMethodメソッドのパラメータを更新] userLocation userLocation)メソッドを想定し

が、これは動作するようになりました(そして、あなたのオブジェクトからあなたの二つの値にアクセスすることができ、「コントローラ名」という名前のコントローラに住んでいる)

+0

私は投稿した後、あなた自身の質問に答えました。どちらの方法でも - 上記はうまくいくはずです:) – Alex