序列化表单数据不asp.net MVC应用程序工作 [英] Serializing form data doesn't work in asp.net mvc app

查看:118
本文介绍了序列化表单数据不asp.net MVC应用程序工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的HTML我有

<h2>Title goes here</h2>

    @using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "myFormID" })) {
        <input type="text" id="testinput" name="testinput" />
        <input type="text" id="testinput2" name="testinput2" />

        <input type="button" value="click me" onclick="submitForm();" />
    }

在我的js我有

function submitForm() { 

    dataString = $("#myFormID").serialize();

    alert(dataString);

    $.ajax({

        type: "POST",
        url: "/Controller/Action",
        data: dataString,
        cache: false,
        dataType: "json",

        success: function (data) {

            alert('success!');
        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + "<br />" + textStatus + "<br />" + errorThrown);

        }

    }); 

}

在我的控制器文件我有:

In my controller file I have:

    public ActionResult Action()
    {
        return View();
    }



    [HttpPost]
    public ActionResult Action(string testinput, string testinput2)
    {
        return View();
    }

在上点击点击我按钮,我得到以下错误:
parsererror
无效的JSON:

When clicked on "click me" button, I get the following error: "parsererror
Invalid JSON: "

我是什么做错了吗?我只是想表单数据传递给jQuery的阿贾克斯。

What am I doing wrong? I am simply trying to pass form data to jquery .ajax.

该警告声明outpusttestinput = gdfgf&安培; testinput2 = gfgfd这是我进入正确的价值观。因此,错误似乎序列化时要....
我使用MVC 3剃刀...我有IM pression的传递模型/视图和JavaScript之间的数据code的变得更加容易。

The alert statement outpust "testinput=gdfgf&testinput2=gfgfd" which is the correct values I entered. SO the error seems to be when serializing.... I am using MVC 3 with razor...I had the impression that passing data between model/view and javascript code was made easier.

推荐答案

我几乎可以肯定有无关,与你传递给 $。阿贾克斯调用数据和一切与 /控制器/动作返回的数据。打开提琴手和检查响应。它很可能是畸形(或JSON不是全部)。

I am almost positive that has nothing to do with the data you pass to the $.ajax call and everything with the data returned by /Controller/Action. Open up Fiddler and examine the response. it is likely malformed (or not JSON at all).

这篇关于序列化表单数据不asp.net MVC应用程序工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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