Ajax返回的数据表总是在asp.net中出现错误 [英] Ajax returning datatable always going in error part in asp.net

查看:64
本文介绍了Ajax返回的数据表总是在asp.net中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 datatable 中返回值列表,并且我想在 dropdownlist 中填写ajax函数的 success 部分.直到返回 dt ,我才能正确地获取所有值,但之后它会出现在 error 部分.下面是我尝试过的.

I return list of values in a datatable and that I want to fill in success part of ajax function in dropdownlist. Till return dt I get all the values properly but after that it goes in error part. Below is what I tried.

Ajax功能

function getMZONEWithState(evt) {

        var ddlState = $('#ContentPlaceHolder1_ddlState').val();

        var ddlMaintenanceZone = $("#ddlMaintenanceZone");

        ddlMaintenanceZone.empty().append('<option selected="selected" value="0" disabled = "disabled">State Loading...</option>');

        $.ajax({
            type: "POST",
            url: "Dashboard.aspx/GetMaintZone",
            data: JSON.stringify({ ddlState: ddlState }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {                   

            },
            error: function (response) {
                alert('Something went wrong..!!');
            }
        });
    }

并在后面的代码中:-

[WebMethod]
    public static DataTable GetMaintZone(string ddlState)
    {   
        DataTable dt = new DataTable();
        try
        {
            CommonDB ObjCommon = new CommonDB();
            dt = ObjCommon.GetMZONE(ddlState);
            return dt;
        }
        catch (Exception)
        {                
            throw;
        }            
    }

为什么它总是出现在我不理解的 error 部分中?请建议我是否在任何地方出错.

Why it always goes in error part I don't understand ?? Please suggest If I am going wrong anywhere.

推荐答案

您不能像这样从您的 [WebMethod] 直接返回 DataTable .您需要先将 DataTable 转换为 JSON ,然后再发送给cleint.

You can't return DataTable directly from your [WebMethod] like this. You need to convert your DataTable to JSON before sending to cleint.

按如下所示更改服务器端代码.

Change your server side code like following.

        [WebMethod]
        public static string GetMaintZone(string ddlState)
        {
            DataTable dt = new DataTable();
            try
            {
                CommonDB ObjCommon = new CommonDB();
                dt = ObjCommon.GetMZONE(ddlState);
                return DataTableToJSON(dt);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string DataTableToJSON(DataTable table)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
            Dictionary<string, object> childRow;
            foreach (DataRow row in table.Rows)
            {
                childRow = new Dictionary<string, object>();
                foreach (DataColumn col in table.Columns)
                {
                    childRow.Add(col.ColumnName, row[col]);
                }
                parentRow.Add(childRow);
            }
            return jsSerializer.Serialize(parentRow);
        }
    }

这篇关于Ajax返回的数据表总是在asp.net中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆