jQuery自带了Ajax的函数,你能够运用它从远程服务器下载网页,但问题是,大部分Ajax都是经过GET那种措施与长途服务器交互的。

 

但假使您想通过Ajax向远程服务器传递数据呢?当然jQuery.Ajax里面有3个参数data参数允许你传递发送到服务器的数量。
但难点是,这些数据默许是应用GET格局,即附加在乞求字符串(UTucsonL QueryString)前面达成的,也便是说你会受到U福睿斯L最大不可能超越4KB的限制。而假若您品尝用POST方法向ASP.NET MVC控制器发送数据的时候,在控制器一端,你会发现数目尚未传来到控制器上。比如说上面包车型大巴jQuery代码:

jQuery 1jQuery 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

jQuery 3jQuery 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 中华VC从前的版本应该就会遇见这么些标题)。那是因为尽管客户端指明了多少是json对象,不过,服务器MVC路由器不知晓什么样解释这么些指标,要在劳动器端加上json反系列化帮衬,请在global.asax.cs的Application_Start()函数里,添加底下一行代码(表示你要协理json反系列化):

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

在ASP.NET MVC 3 TucsonC现在的本子就不须要加上这一行代码了。

相关文章

网站地图xml地图