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?
Wenn Sie den Antworttext des Antrags in der Konsole überprüfen, was ist die genauen Fehler zur Verfügung gestellt? –
HI Rory Ich erhalte diesen Fehler in der Konsole "Antwort für Preflight hat ungültigen http Statuscode 405" – Vikash
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. –