简体   繁体   中英

Failed to load resource: the server responded with a status of 405 () and mysterius input

I have this function

function AddComment(id) {
    var input = $("#" + "CommentOnPost" + id).val();
  
    var commentHolder = $("#commentDiv" + id);
    commentHolder.empty();

    $.ajax({
        url: 'Account/AddCommentToPost',
        data: { postId: id, text:input },
        dataType: 'json',
        cache: false,
        success: function (result) {
            //irrelevant
        },
    });
}

But when debugging it, I can see that is makes the following request:

https://localhost:44398/Account/AddCommentToPost?postId=1&text=gd&_=1596616234410

That extra parameter "_" should not be there and is perhaps causing the problem?

If you applied [HttpPost] attribute to an action, which would identify your action that supports the HTTP POST method only. Your Ajax code snippet would make ' GET ' request, which cause "405 Method Not Allowed" error.

To fix it, as you mentioned, you can try to set type option with 'POST'.

$.ajax({
    url: 'Account/AddCommentToPost',
    type: 'POST',
    //...

Or remove [HttpPost] attribute from your action method to make it support both GET and POST request(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM