采取jQuery.Ajax向ASP.NET MVC控制器Post数据

jQuery自带了Ajax的函数,你得应用其打远程服务器下载网页,但问题是,大部分Ajax都是由此GET这种方式以及远程服务器交互的。

 

可是只要你想通过Ajax向远程服务器传递数据呢?当然jQuery.Ajax里面有一个参数data参数允许而传递发送到服务器的多少。
但问题是,这个数目默认是使用GET方式,即附加以请字符串(URL QueryString)后面实现之,也就是说你见面面临URL最可怜未可知超过4KB的克。而使你品味用POST方法为ASP.NET MVC控制器发送数据的时节,在控制器一端,你晤面发现数尚未传来到控制器上。比如说下面的jQuery代码:

图片 1图片 2代码

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
    <title>Tools</title>
    <link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css” rel=”stylesheet” type=”text/css”/>
    <LINK rel=”stylesheet” href=”http://jqueryui.com/demos/demos.css” />
    <script type=”text/javascript” src=”Scripts/jquery-1.4.2.min.js”></script>
    <script type=”text/javascript” src=”Scripts/jquery-ui-1.8.6.custom.min.js”></script>
    <script type=”text/javascript” src=”Scripts/tinymce/tiny_mce.js”></script>
    <script type=”text/javascript” src=”Scripts/tinymce/jquery.tinymce.js”></script>
</script>
    <script language=”javascript” type=”text/javascript”>
        var editor_selector = “#note_editor”;
        var annotation_catalog_url = ‘/Comment/Save’;

        function show_annotation_dialog() {
            $(editor_selector).tinymce({
                mode: “textareas”,
                theme: “simple”
            });
        }

        function save_handler() {
            if (tinyMCE.get(‘note_editor’)) {
                var html = tinyMCE.get(‘note_editor’).getContent();
                var comment = {
                        Id: -1,
                        User: ”,
                        PageUrl: ” + window.location.href,
                        Content: html
                };

                $.ajax({
                    url: annotation_catalog_url,
                    type: ‘POST’,
                    dataType: ‘json’,
                    data: comment,
                    contentType: ‘application/json; charset=utf-8’,
                    success: function (data) {
                        if (data != null)
                            $(‘#result’).html(data.Message);
                    }
                });
            }
        }

        $(document).ready(function () {
            $(‘#submit’).click(save_handler);            
        });
    </script>
</head>
<body>
   <div id=”note_editor”>
   </div>
   <input type=”button” value=”提交” id=”submit” />   

   <div id=”result”>
   </div>
</body>

 

点击提交后,在控制器一端根本无受调用,这是因尽管您的jQuery代码指明了数据类型是json,但是多少格式并无是json格式的。你待以目标序列化成json格式,使用json2.js不怕足以举行此工作,下载地址是:http://www.json.org/js.html 。

图片 3图片 4代码

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
    <title>Tools</title>
    <link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css” rel=”stylesheet” type=”text/css”/>
    <LINK rel=”stylesheet” href=”http://jqueryui.com/demos/demos.css” />
    <script type=”text/javascript” src=”Scripts/jquery-1.4.2.min.js”></script>
    <script type=”text/javascript” src=”Scripts/jquery-ui-1.8.6.custom.min.js”></script>
    <script type=”text/javascript” src=”Scripts/tinymce/tiny_mce.js”></script>
    <script type=”text/javascript” src=”Scripts/tinymce/jquery.tinymce.js”>
    <script type=”text/javascript” src=”Scripts/json2.js”></script>
</script>
</script>
    <script language=”javascript” type=”text/javascript”>
        var editor_selector = “#note_editor”;
        var annotation_catalog_url = ‘/Comment/Save’;

        function show_annotation_dialog() {
            $(editor_selector).tinymce({
                mode: “textareas”,
                theme: “simple”
            });
        }

        function save_handler() {
            if (tinyMCE.get(‘note_editor’)) {
                var html = tinyMCE.get(‘note_editor’).getContent();
                var comment = {
                        Id: -1,
                        User: ”,
                        PageUrl: ” + window.location.href,
                        Content: html
                };

                $.ajax({
                    url: annotation_catalog_url,
                    type: ‘POST’,
                    dataType: ‘json’,
                    data: JSON.stringify(comment),
                    contentType: ‘application/json; charset=utf-8’,
                    success: function (data) {
                        if (data != null)
                            $(‘#result’).html(data.Message);
                    }
                });
            }
        }

        $(document).ready(function () {
            $(‘#submit’).click(save_handler);            
        });
    </script>
</head>
<body>
   <div id=”note_editor”>
   </div>
   <input type=”button” value=”提交” id=”submit” />   

   <div id=”result”>
   </div>
</body>

设若您以服务器的控制器一端,发生多少尚未于正确地反序列化(如果您用ASP.NET MVC 3 RC之前的版本应该就是会赶上这个题材)。这是坐尽管客户端指明了数是json对象,但是,服务器MVC路由器不清楚哪说这目标,要以劳务器端加上json反序列化支持,请在global.asax.cs的Application_Start()函数里,添加底下一行代码(表示您要支持json反序列化):

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

在ASP.NET MVC 3 RC以后的版就不需要丰富这一行代码了。

相关文章