Ajax应用jQuery.Ajax向ASP.NET MVC控制器Post数据

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

jQuery自带了Ajax的函数,你可以选择它从远程服务器下载网页,但难点是,半数以上Ajax都是经过GET那种措施与长途服务器交互的。

<!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’;

<!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 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 1Ajax 2代码

                $.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);
                    }
                });
            }
        }

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

 

倘使您在服务器的控制器一端,爆发多少没有被正确地反连串化(假使你用ASP.NET MVC 3 RC从前的本子应该就会遇上那个问题)。这是因为即便客户端指明了数据是json对象,可是,服务器MVC路由器不知情如何诠释这些目的,要在劳动器端加上json反系列化帮助,请在global.asax.cs的Application_Start()函数里,添加底下一行代码(表示你要帮助json反种类化):

                $.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);            
        });
Ajax,    </script>
</head>
<body>
   <div id=”note_editor”>
   </div>
   <input type=”button” value=”提交” id=”submit” />   

但只要你想透过Ajax向远程服务器传递数据呢?当然jQuery.Ajax里面有一个参数data参数允许你传递发送到服务器的数目。
但难点是,这么些数额默许是利用GET格局,即附加在呼吁字符串(URL QueryString)后边落成的,也就是说你会蒙受URL最大不可以当先4KB的范围。而要是您品味用POST方法向ASP.NET MVC控制器发送数据的时候,在控制器一端,你会意识数目没有传到到控制器上。比如说上边的jQuery代码:

在ASP.NET MVC 3 RC将来的本子就不要求丰富这一行代码了。

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

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

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

Ajax 3Ajax 4代码

        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
                };

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

点击提交后,在控制器一端根本未曾被调用,那是因为即使你的jQuery代码指明了数据类型是json,但是多少格式并不是json格式的。你要求将对象体系化成json格式,使用json2.js就足以做那么些工作,下载地址是:http://www.json.org/js.html

相关文章