DTCMS使用ajax局部刷新

动力启航的DTCMS代码遇到的问题:

前台post请求:

$.ajax({

                type: "POST",

                url: sendUrl,

                dataType: "json",

                timeout: 20000,

               

                success: function (data) {



                    if (data != null) {

                        $("#ul").html("");

                        for (var i = 0; i < data.json_data.length; i++) {

                            var item = data.json_data[i];

                            var tobody = "";

                           

                            $(ul).append(tobody);

                        }



                    };

                },

                error: function (XMLHttpRequest, textStatus, errorThrown) {

                    alert(textStatus);

                }

            });

后台转换为JSON格式:

/// <summary>  
        /// DataTable to json  
        /// </summary>  
        /// <param name="jsonName">返回json的名称</param>  
        /// <param name="dt">转换成json的表</param>  
        /// <returns></returns>  
        public static string DataTableToJson(DataTable dt, string pagelist)
        {
            StringBuilder JsonString = new StringBuilder();

            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"json_data\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1 && j!=11)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString().Replace("\"", "\\\"") + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString().Replace("\"", "\\\"") + "\":" + "\"" + dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }


                }
                JsonString.Append("], ");
                JsonString.Append("\"pagelist\":[{ \"pagelist\":");
                JsonString.Append("\"" + pagelist.Replace("\"", "\'") + "\"");
                JsonString.Append("}");
                JsonString.Append("]");
                JsonString.Append("}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

转换为json格式时的调用:

string json;

json = DataTableToJson(dt,pagelist);

其中ds 为datatable类型的。

你可能感兴趣的:(Ajax)