2017-02-27 10 views
0

jQuery AJAXで呼び出す必要があるWebAPIメソッドを1つ使用しています。以下は、AJAX呼び出しに使用するjQueryのコードです:WebAPIメソッドを使用しないAJAX呼び出し

var BlogAndStoryComment = new Object(); 
BlogAndStoryComment.CommentID = 0; 
BlogAndStoryComment.CommentUserName = userName; 
BlogAndStoryComment.CommentText = commentText; 
BlogAndStoryComment.CommentApprovedByUserID = 0; 
BlogAndStoryComment.CommentDate = "date"; 
BlogAndStoryComment.HtmlComment = commentHtml; 
BlogAndStoryComment.CommentIsSpam = 0; 
BlogAndStoryComment.CommentIsApproved = 0; 
BlogAndStoryComment.CommentEmail = email; 
BlogAndStoryComment.CommentCount = 0; 
BlogAndStoryComment.OnCommentID = 0; 
BlogAndStoryComment.BlogID = blogID; 
BlogAndStoryComment.SiteID = siteID; 
BlogAndStoryComment.RowCount = 0; 

$.ajax({ 
    url: "http://localhost:55052/API/comments/GetAndPostBlogComments", 
    type: "POST", 
    data: JSON.stringify(BlogAndStoryComment), 
    contentType: 'application/json; charset=utf-8', 
    dataType: "json", 
    success: function(response) {}, 
    error: function(jqXHR, textStatus, errorThrown) {}, 
    failure: function(response) {} 
}); 

これは私のWebAPIの方法である:

[Route("api/comments/GetAndPostBlogComments")] 
[VersionedRoute("", 1)] 
[ResponseType(typeof(HttpResponseMessage))] 
[HttpPost] 
public IHttpActionResult GetAndPostBlogComments([FromBody] BlogAndStoryComment comment) 
{ 
} 

私はAJAXからこのメソッドを呼び出すと、それが与えるerror機能に当たっている呼び出します私はstatustextまたは"error"です。しかし、私がPostmanを呼び出すと、メソッドは正しく動作しています。どうした?あなたはJSON形式で送信する場合は、あなただけの定義クライアント側で

public void Post([FromBody]string name) 
{ 
} 

、::サーバ側でシンプルなタイプについては、

+1

を使用することができますか? –

+0

HI Roryコンソールでこのエラーが発生しました "プリフライトの応答に無効なHTTPステータスコード405があります" – Vikash

+0

このエラーは、リクエストがクロスドメインであると解釈されていることを意味します。 JSコードは同じ 'http:// localhost:55052'のURLで動作していますか?もしそうでなければ、それはあなたの問題です。 CORSヘッダーを応答に追加するようにAPIを修正する必要があります。 –

答えて

0

var dataJSON = "test"; 

    $('#testPostMethod').bind("click", GeneralPost); 
    function GeneralPost() { 
     $.ajax({ 
      type: 'POST', 
      url: '/api/comments/GetAndPostBlogComments', 
      //url: '../api/comments/GetAndPostBlogComments', 
      //url: '~/api/comments/GetAndPostBlogComments', 
      data: JSON.stringify(dataJSON), 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json' 
     }); 
    } 

あなたはそれを動作させるにしたい場合サーバー側からの、複合型であなたが定義する必要があります。

public class RecipeInformation 
{ 
    public string name { get; set; } 
} 

public class ValuesController : ApiController 
{ 
    public void Post(RecipeInformation information) 
    { 
    } 
} 

そして、クライアント側からの:

var dataJSON = { name: "test" }; 

    $('#testPostMethod').bind("click", GeneralPost); 
    function GeneralPost() { 
     $.ajax({ 
      type: 'POST', 
      url: '/api/comments/GetAndPostBlogComments', 
      //url: '../api/comments/GetAndPostBlogComments', 
      //url: '~/api/comments/GetAndPostBlogComments', 
      data: JSON.stringify(dataJSON), 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json' 
     }); 
    } 
0

あなたはこのような何かをやってみてください、あなたは正確なエラーが提供されるものコンソールでの要求の応答テキストを確認した場合はjqueryののparamメソッド

var postData = { 
     name : 'name' 
    } 

    $('#testPostMethod').bind("click", GeneralPost); 
    function GeneralPost() { 
     $.ajax({ 
      type: 'POST', 
      url: '../api/comments/GetAndPostBlogComments', 
      //url: '~/api/comments/GetAndPostBlogComments', 
      data: $.param(postData,true), 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json' 
     }); 
    } 
関連する問題