前台用 JQUERY AJAX POST 传送了一个 JSON 字符串,后台怎么接收这个字符串呢?
代码如下:
  
                 $("#btn").click(function(){
                 var json="{\"data:\"{"
                    $('#cartProducts .productCart').each(function()
                     {
                            json+="[\"cartPName \":"+$('#cartProductName', this).html();
                            
                             json+=",\"cartQuantity\":"+parseInt($('span.quantity', this).html());
                      });
                     json+="}"
                    
                 $.ajax({
                 type:"POST",
                 url:"Default3.aspx",
                 dataType:"json",
                 data:json,
                  success:function(data){}
                  });

解决方案 »

  1.   

    本帖最后由 net_lover 于 2010-11-26 14:50:46 编辑
      

  2.   

    你确认你提交的数据准确
    <%@ Page Language="C#" %><script runat="server">  
      protected void Page_Load(object sender, EventArgs e)
      {
        if (Request.RequestType == "POST")
        {
          Response.ClearContent();
          Response.ContentType = "text/html";
          System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream);
          string test;
          test = reader.ReadToEnd();
          Response.Write(test);
          reader.Dispose();
          Response.End();     
        }
      }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <script type="text/javascript" src="jquery-1.4.2.js"></script>
      <script>
        function xxx() {
          var json = '{"a":"b"}';
          $.ajax({
            type: "POST",
            url: "<%= Request.FilePath %>",
            dataType: "html",
            data: json,
            success: function (result) {
              alert("您提交的数据:"+result)
            }
          });
        }
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
        <input id="btn" type="button" value="button" onclick="xxx()" />
      </form>
    </body>
    </html>
      

  3.   

    为什么非得用JSON?
    不用可不可以呀.
      

  4.   

    dataType是返回的类型,不是提交的类型
      

  5.   

    啊,我以前用ajax+webservice,每次都传一堆字符串,觉得太麻烦,想改成传json,但是搞了很久得出的结论是没有办法直接解析,还要用什么第三方控件之类的,然后就放弃了
      

  6.   

    用DataContractJsonSerializer,mvc的话只要参数名称一样就能自动传了
      

  7.   


      $.ajax({
                                type: "POST",
                                contentType: "application/json; charset=utf-8",
                                url: "/Line/AddLineMaster/",
                                data: $.toJSON(TestLineMaster),   // 序列化JSON对象,用了一个叫 jquery-json 的插件
                                dataType: "json",
                                success: function (json) {
                                    SearchLine();
                                }
                            });    [JsonParamFilter(TargetType = typeof(LineMaster), Param = "lineMaster")]
            public ActionResult AddLineMaster(LineMaster lineMaster)
            {            
     
            }
    json 可以用过插件转换整这种结构,
    下边是 lineMaster在Js段的声明function lineMaster(id, lineName, totalDay, totalCost, feature, lineDescription, compiler, createDate, editDate, stations) {
        this.ID = id, //标示
        this.LineName = lineName, 
        this.TotalDay = totalDay, 
        this.TotalCost = totalCost, 
        this.Feature = feature, 
        this.LineDescription = lineDescription, 
        this.Compiler = compiler,
        this.CreateDate = createDate, 
        this.EditDate = editDate, 
        this.Stations = stations
    }调用方式    var NewLineMaster = new lineMaster("00000000-0000-0000-0000-000000000000", "", 0, 0, "", "",
       "00000000-0000-0000-0000-000000000000", NowTime, NowTime, new Array());
        reValidator();
      

  8.   

    这个是 asp.net mvc 2.0的代码,但是其他框架的代码应该也是类似的。