MVC5 controller action not called from JSON AJAX Post

后端 未结 2 1509
情话喂你
情话喂你 2020-12-12 05:41

I am sending data from from a javascript app to a MVC5 controller, however when data is submitted to the Submit controller action, it is never called. I have some very simpl

相关标签:
2条回答
  • 2020-12-12 06:18

    Four things you must check using ajax calls,
    1. If using javascript object you must stringify the object before passing.
    2. The Action verb for the action method should be same as the type of your ajax call if POST then the action method should be decorated by action verb [HttpPost].
    3. Always use the relative path for url's in ajax as @Url.Action("action", "controller").
    4. The input parameters of your action method method should match the json object parameters (exactly i.e. case sensitive).

    For debugging you may use firebug addon in your browser so that you can see what is sent over the network or press F12 for debugging tool in that check in network tab.

    0 讨论(0)
  • 2020-12-12 06:24

    You will have to make two changes: Stringify your Json as below:

    $.ajax({
        cache: false,
        url: url,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(item),
        type: 'POST',
        success: function (data, textStatus, jqXHR) {           
        },
        error: function (data, textStatus, jqXHR) { 
        }
    });
    

    Second, Just Annotate your Method with [HttpPost] as below:

    [HttpPost]
    public JsonResult Submit(string[] Skus, ContactDto Contact)
    {
        return Json(new { success = true, message = "Some message" });
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题