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以后的本就无欲添加这一行代码了。

相关文章

网站地图xml地图