jQuery Ajax帖子没有传递参数

Sco*_*ott 5 c# ajax asp.net-mvc jquery json

我正在尝试使用jQuery的$ .ajax()函数将表单变量发布到MVC路由.问题是,当代码命中我的MVC操作时,即使数据传递给它们,所有参数都为null:

jQuery的:

$(function () {
    $('#signupform').submit(function (e) {
        e.preventDefault();

        if ($(this).valid()) {
            var postData = '{name : "' + $("#Name").val() + '", email : "' + $("#Email").val() + '", message : "' + $("#Message").val() + '" }';

            $.ajax({
                url: "/api/contact-form-post",
                data: postData,
                type: "get"
            })
            .complete(function (data) {
                $("#formContainer").html($("#formThankYou").html());
            });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

调用alert(postData)输出以下内容:

{name : "Scott Smith", email : "scott@smith.com", message : "test message" }
Run Code Online (Sandbox Code Playgroud)

MVC行动:

public JsonResult ContactFormPost(string email, string name = "" , string message = "")
        {
            AddEmailToMailingList(email);

            if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(message))
            {
                InsertContactMessage(email, name, message);
            }

            return Json(true);
        }
Run Code Online (Sandbox Code Playgroud)

使用FireBug检查请求会显示这是被调用的URL.显然url参数的格式不正确,但我无法弄清楚原因.

http:// localhost:10637/api/contact-form-post?{name%20:%20%22Scott%20Smith%22,%20email%20:%20%22scott @ smith.com%22,%20message%20 :%20%22Test%20message%22%20}

我在这里犯了任何明显的错误会导致我的ContactFormPost方法的参数始终为null吗?

Tri*_*dad 12

取消引用postData,将$ .ajax传递给一个真正的JS对象.

var postData = {
    name: $("#Name").val(),
    email: $("#Email").val(),
    message: $("#Message").val()
};
Run Code Online (Sandbox Code Playgroud)