JsonResult方法不通过AJAX调用调用

时间:2017-08-17 16:35:06

标签: jquery ajax asp.net-mvc jsonresult

json输出的链接:http://localhost:50028/Account/

我的JsonResult方法如下:

[HttpGet]
    public JsonResult GetGroupList()
    {
        try
        {
            DataConnection store = new DataConnection();
            DataTable dt = store.GetDataTable("GetGroupList");
            if (dt.Rows.Count > 0)
            {
                var result = new { Success = "true", Message = Common.SerializeDataTable(dt) };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Success = "false", Message = "There is no item in Group list." };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }
        catch (Exception ex)
        {
            Common.InsertErrorLog("AccountController", "GetGroupList()", ex.ToString());
            var result = new { Success = "false", Message = "Internal Error, Exception occured." };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }

以上mvc jsonresult方法的输出

{
"Success": "true",
"Message": "[{\"ID\":1,\"TEXT\":\"BANK ACCOUNT\"},{\"ID\":2,\"TEXT\":\"BANK O/D ACCOUNT\"},{\"ID\":3,\"TEXT\":\"CAPITAL ACCOUNT\"},{\"ID\":4,\"TEXT\":\"CASH IN HAND\"},{\"ID\":5,\"TEXT\":\"CURRENT ASSETS\"},{\"ID\":6,\"TEXT\":\"CURRENT LIABILITIES\"},{\"ID\":7,\"TEXT\":\"DIRECT EXPENSES\"},{\"ID\":8,\"TEXT\":\"DIRECT INCOMES\"},{\"ID\":9,\"TEXT\":\"FIXED ASSETS\"},{\"ID\":10,\"TEXT\":\"INDIRECT EXPENSES\"},{\"ID\":11,\"TEXT\":\"INDIRECT INCOME\"},{\"ID\":12,\"TEXT\":\"INVESTMENT\"},{\"ID\":13,\"TEXT\":\"SECURED LOAN\"},{\"ID\":14,\"TEXT\":\"UNSECURED LOAN\"},{\"ID\":15,\"TEXT\":\"PURCHASE ACCOUNTS\"},{\"ID\":16,\"TEXT\":\"SALES ACCOUNT\"},{\"ID\":17,\"TEXT\":\"STOCK IN HAND\"},{\"ID\":18,\"TEXT\":\"SUNDRY CREDITORS\"},{\"ID\":19,\"TEXT\":\"SUNDRY DEBTORS\"},{\"ID\":20,\"TEXT\":\"PROVISIONS\"},{\"ID\":21,\"TEXT\":\"DUTIES AND TAXES\"},{\"ID\":22,\"TEXT\":\"DEPOSIT ACCOUNT\"},{\"ID\":23,\"TEXT\":\"LOANS AND ADVANCES\"},{\"ID\":24,\"TEXT\":\"LOANS LIABILITIES\"},{\"ID\":25,\"TEXT\":\"BANK OCC\"},{\"ID\":26,\"TEXT\":\"BRANCH/DIVISIONS\"},{\"ID\":27,\"TEXT\":\"MISC. EXPENSE (ASSET)\"},{\"ID\":28,\"TEXT\":\"SUSPENSE A/C\"},{\"ID\":29,\"TEXT\":\"RESERVE & SURPLUS\"},{\"ID\":30,\"TEXT\":\"OPENING STOCK\"},{\"ID\":31,\"TEXT\":\"PROFIT & LOSS A/C\"}]"
}

我的ajax电话如下:

$(document).ready(function () {
        $.ajax({
            type: 'GET',
            cache: false,
            url: "http://localhost:50028/Account/GetGroupList",
            //url: "http://api.ispecial.in/Account/GetGroupList/",
            async: true,
            contentType: 'application/json; charset=utf-8',
            dataType: 'Json',
            success: function (response) {
                alert('1');
                //debugger;
                var res = JSON.parse(response).Table;
                console.log(res);
                //alert(response);
                //var res = JSON.parse(response);
                //alert(res);
                //$("#tbGroupList").append("<tr><td>" + data.d[i].ID + "</td><td>" + data.d[i].TEXT + "</td></tr>");
            },
            error: function (response) {
                alert('error');
                console.log(response);
            }
        });
    });

我创建了JsonResult [HttpGet]方法,通过ajax调用我想得到输出,但是我收到了错误。

1 个答案:

答案 0 :(得分:0)

根据我们在聊天中的讨论,使用下面的代码来解决问题。

$(document).ready(function () {
        $.ajax({
            type: 'GET',
            cache: false,
            url: "http://api.ispecial.in/Account/GetGroupList",
            dataType: 'Json',
            success: function (response) {
                alert('1');
                console.log(response);
            },
            error: function (response) {
                alert('error');
                console.log(response);
            }
        });
});

如果要从响应中提取Message,请使用response.Message