2017-02-27 5 views
0

Ich verwende eine WebAPI-Methode, die über jQuery AJAX aufgerufen werden muss. Unten ist der jQuery-Code für den AJAX-Aufruf verwendet:AJAX-Aufruf nicht WebAPI-Methode

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) {} 
}); 

Das ist meine WebAPI Methode ist:

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

Wenn ich diese Methode von der Ajax-Aufruf nennt es die error Funktion trifft, die gibt mir ein statustext oder "error". Wenn ich jedoch Postman anrufe, funktioniert die Methode korrekt. Was ist das Problem?

+1

Wenn Sie den Antworttext des Antrags in der Konsole überprüfen, was ist die genauen Fehler zur Verfügung gestellt? –

+0

HI Rory Ich erhalte diesen Fehler in der Konsole "Antwort für Preflight hat ungültigen http Statuscode 405" – Vikash

+0

Dieser Fehler bedeutet, dass die Anfrage als Cross-Domain interpretiert wird. Wird der JS-Code auf derselben URL "http: // localhost: 55052" ausgeführt? Wenn nicht, ist das dein Problem. Sie müssen die API so anpassen, dass sie der Antwort CORS-Header hinzufügt. –

Antwort

0

Für einfache Art, auf Server-Seite:

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

auf der Client-Seite, die Sie gerade definieren, wenn Sie im JSON-Format senden möchten:

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' 
     }); 
    } 

Wenn Sie es machen wollen arbeiten in komplexem Typ von der Server-Seite sollten Sie definieren:

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

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

Und von Client-Seite:

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

können Sie versuchen, so etwas wie dies zu tun und nutzen die jQuery param Methode

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' 
     }); 
    }