通常,通过AJAX向后台发送数据,我们可以通过POST方式发送
而这时要发送的内容,必须按 id=123&name=XXX 这样的格式传送。而且传送的内容还要进行适当的编码
比如特殊字符 & ,空格,+ 等等,都要进行转换
而如果可以直接传送JSON内容就方便多了,代码如下,PHP后台,其它后台方法类似前台页面<html>
<head>
    <title>JSON</title>
    <!--JSON2.js可以去json.org去找,这个JS应该经得起各种考验-->
    <script src="JSON2.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        //获取XMLHttpRequest
        function GetHttpRequest() {
            var xmlHttp = false;
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e2) { xmlHttp = false; }
            }
            if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                xmlHttp = new XMLHttpRequest();
            }
            return xmlHttp;
        }
        
        //传递一个JSON对象到服务器,此例为一个数组 
        //通常在提交多条数据时,可以用循环提交多次,现在只要一次就行了 
        var datas=[
            {userID:1,userName:"user 1",re:"中文内容1"},
            {userID:2,userName:"user 2",re:"中文内容2"}
        ];
        
        var xmlHttp;
        window.onload=function(){
            xmlHttp=GetHttpRequest();
            xmlHttp.open("POST","test.php");
            //通常,Content-Type的内容会被默认设置为application/x-www-form-urlencoded
            //也就是以表单变量的方式传送,以便后台可以通过POST方式直接获取变量 
            //在此,我们改为text/plain 
            //所以xmlHttp.send的内容也不必按 id=124&name=jack 这样的格式传送 
            //直接将JSON内容当普通文本传送 
            xmlHttp.setRequestHeader("Content-Type","text/plain");
            xmlHttp.onreadystatechange=function(){
                if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
                    //页面正确返回数据 
                    var result=xmlHttp.responseText;
                    //作为示例,仅作简单显示 
                    document.getElementById("output").innerText=result;
                }
            };
            //直接传送JSON的内容 
            xmlHttp.send(JSON.stringify(datas));
        };
    </script>
</head>
<body>
    从服务器返回的数据是:<br/>
    <pre id="output"></pre>
</body>
</html>
后台服务<?php
//直接读取POST过来的内容,而不是用$_POST 
//而其它后台(ASP.NET,JAVA等...),各有各自的方法实现这种读取 
$content=file_get_contents("php://input");//将内容转换为JSON对象,以数组的形式转换 
//因AJAX传送的内容是UTF-8,所以在此可以正确转换 
$json=json_decode($content,true);//如果输出时,也按JSON格式输出,即可实现AJAX到后台全部用JSON进行交互 
//作为示例,直接输出内容 
print_r($json);
?>

解决方案 »

  1.   


    var obj={
                id1: "",
                id2: ""
            };
    function funTest() {
                obj.id1= $("#<%= this.id1.ClientID %>").val();
                obj.id2 = $("#<%= this.id2.ClientID %>").val();
                $.ajax({
                    type: 'POST',
                    url: 'Js/xxxx.aspx',
                    dataType: 'json',
                    async: false,
                    data: obj,
                    success: function (msg) {
                        }
                     });
                 }一般是通过传递一个对象过去吧 
      

  2.   

    JSON.stringify这个应该是HTML5的吧
    我在公司系统的后台就用这个,但IE早期版本IE是不支持的,据说IE9没问题,还没测
      

  3.   

    我导入这个json2.js老是提示错误,唉
      

  4.   

    json2.js中定义的JSON对象,在HTML5中已经内嵌了,例如在chrome中不需要json2.js就可以直接使用JSON.stringify,而IE8就需要
    而json2.js的引入也不会破坏原有的JSON。