2017-10-31 10 views
2

私のAJAXがhttpポストコントローラにデータを送信していません。asp.net ajaxがデータを送信していません

マイコントローラ:

[Route("api/sendingData")] 
public class TestController : ApiController 
{ 
    [HttpPost] 
    public string Post([FromBody] int propertyID) 
    { 
     return string.Format("Test"); 
    } 
} 

私のAJAX:

$.ajax(
    { 
     url: "api/sendingData", 
     type: "POST", 
     dataType: 'json', 
     data: { 
      'propertyID': '1' 
     }, 
     success: function (result) { 
      console.debug(result); 
      alert(result); 
     }, 
     error: function (xhr, status, p3, p4) { 
      console.debug(xhr); 
      var err = "Error " + " " + status + " " + p3; 
      if (xhr.responseText && xhr.responseText[0] == "{") 
       err = JSON.parse(xhr.responseText).message; 
      alert(err); 
     } 
    }); 

私はpropertyID=1を送信しようとしています。しかし、コントローラをデバッグすると、propertyID=0と表示されます。

何が間違っているか知っていますか?

+0

これをテストしてください: 'data:{propertyID:JSON.stringify( '1')}' –

+0

ありがとう。しかし、それでも同じ問題 – user6824563

+0

一重引用符を削除してみてください:data:{propertyID:1}、 – Sparrow

答えて

2

は奇妙に見えるかもしれませんが、あなたは一つの値だけではなく、モデルを送っているので、文字列化がJSON.stringify(値)

var propertyID = 1; 
$.ajax({ 
    url: "api/sendingData", 
    contentType: 'application/json', 
    type: 'POST', 
    data: JSON.stringify(propertyID), 
    success: function (result) { 
     console.debug(result); 
     alert(result); 
    }, 
    error: function (xhr, status, p3, p4) { 
     console.debug(xhr); 
     var err = "Error " + " " + status + " " + p3; 
     if (xhr.responseText && xhr.responseText[0] == "{") 
      err = JSON.parse(xhr.responseText).message; 
     alert(err); 
    } 
}); 

であるあなたのアクションメソッドが返すされていないため、また、私はデータ型JSONを削除しましたjsonしかし文字列。今、私は成功を収めています。

関連する問題