具有输入参数的MVC jquery ajax调用 [英] MVC jquery ajax call with input parameter

查看:72
本文介绍了具有输入参数的MVC jquery ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有参数的控制器上调用动作时遇到问题

I'm facing a problem when calling an action on a controller with an argument

在控制器中,我有以下操作:

In the controller i have following action:

[HttpPost]
public ActionResult UpdateData(string month)
{
    return Json(new { success = true, message = "Hello world" });
}

视图中的ajax调用如下:

The ajax call in the view looks like:

$.ajax({
    url: '@Url.Action("UpdateData")',
    dataType: "json",
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    cache: false,
    data: {  },
    success: function (data) {
        if (data.success) {
            alert(data.message);
        }
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
});

这很好用,但是当我想传递

this works well, but when i want to pass the argument like

data: {month:'may'} 

我收到一个错误,该操作从未调用.我只是看不到.

i get an error, the action is never called. I just don't see it.


好吧,我只需要将json作为字符串传递:

Ok, i just had to pass the json as a string:

data: "{month:'may'}"

我认为我需要更多的睡眠:-)

I think i need more sleep :-)

推荐答案

如果您在网址:

 $.ajax({
        url: '@Url.Action("UpdateData","YourControllerName")',
        dataType: "json",
        type: "POST",            
        cache: false,
        data: {month:'may'} ,
        success: function (data) {
            if (data.success) {
                alert(data.message);
            }
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
});

这篇关于具有输入参数的MVC jquery ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆