请求帮助:ajax调用后台方法返回页面显示为undefined

时间:2021-12-16 03:38:17
前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "Post",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "html",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static string SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        //将结果转换成Json字符串
        string result = serializer.Serialize(list);
        return result;
    }[WebMethod] 
    public static string SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        //将结果转换成Json字符串
        string result = serializer.Serialize(list);
        return result;
    }

14 个解决方案

#1


先打个断点看下后台输出的result是什么,如果输出结果没问题的,那就试试把dataType改成json格式

#2


引用 1 楼 csl_1022 的回复:
先打个断点看下后台输出的result是什么,如果输出结果没问题的,那就试试把dataType改成json格式

result输出的是表的字段和值,把dataType改成json格式还是不行

#3


html--->text
试试

#4


引用 3 楼 sibiyellow 的回复:
html--->text
试试

这个也不行

#5


result返回的值是: 请求帮助:ajax调用后台方法返回页面显示为undefined

#6


前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

#8


引用 6 楼 QQ234788028 的回复:
前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC

#9


$.each(data.d, function (i, item) {

#10


引用 9 楼 xdashewan 的回复:
$.each(data.d, function (i, item) {

报错

#11


引用 8 楼 DQJA13 的回复:
Quote: 引用 6 楼 QQ234788028 的回复:

前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC


$.each(data, function (i, item) {
这样前面加一个
data=eval(data);

#12


引用 11 楼 QQ234788028 的回复:
Quote: 引用 8 楼 DQJA13 的回复:

Quote: 引用 6 楼 QQ234788028 的回复:

前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC


$.each(data, function (i, item) {
这样前面加一个
data=eval(data);

报错:缺少‘;’

#13


如何在jQuery或javascript处理接收到json数据。可以参考:
http://www.cnblogs.com/insus/p/3713459.html

#14


你可以在后台直接把table的字符串组合完成,然后发给前台显示就完事了嘛

#1


先打个断点看下后台输出的result是什么,如果输出结果没问题的,那就试试把dataType改成json格式

#2


引用 1 楼 csl_1022 的回复:
先打个断点看下后台输出的result是什么,如果输出结果没问题的,那就试试把dataType改成json格式

result输出的是表的字段和值,把dataType改成json格式还是不行

#3


html--->text
试试

#4


引用 3 楼 sibiyellow 的回复:
html--->text
试试

这个也不行

#5


result返回的值是: 请求帮助:ajax调用后台方法返回页面显示为undefined

#6


前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

#7


#8


引用 6 楼 QQ234788028 的回复:
前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC

#9


$.each(data.d, function (i, item) {

#10


引用 9 楼 xdashewan 的回复:
$.each(data.d, function (i, item) {

报错

#11


引用 8 楼 DQJA13 的回复:
Quote: 引用 6 楼 QQ234788028 的回复:

前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC


$.each(data, function (i, item) {
这样前面加一个
data=eval(data);

#12


引用 11 楼 QQ234788028 的回复:
Quote: 引用 8 楼 DQJA13 的回复:

Quote: 引用 6 楼 QQ234788028 的回复:

前台代码:
function start() {
            $.ajax({
                //要用post方式      
                type: "get",
                //方法所在页面和方法名      
                url: "Second.aspx/SelectAll",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    t = "<table>";
                    t += "<tr>";
                    t += "<td>员工编号</td><td>员工姓名</td><td>工作</td><td>上级编号</td><td>受雇日期</td><td>薪金</td><td>佣金</td><td>所属部门</td><tr>";
                    $.each(data, function (i, item) {
                        t += "<tr>";
                        t += "<td>" + item.EMPNO + "</td>";
                        t += "<td>" + item.ENAME + "</td>";
                        t += "<td>" + item.JOB + "</td>";
                        t += "<td>" + item.MGR + "</td>";
                        t += "<td>" + item.HIREDATE + "</td>";
                        t += "<td>" + item.SAL + "</td>";
                        t += "<td>" + item.COMM + "</td>";
                        t += "<td>" + item.DEPTNO + "</td>";
                        t += "</tr>";
                    });
                    t += "</table>";
                    $("#table").html(t);
                },
                error: function (err) {
                    alert(err);
                }
            });     
        }
后台代码:
[WebMethod] 
    public static JsonResult SelectAll()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        //接收查询结果
        List<EMP> list = EMPManager.SelectAll();
        return Json(list);
    }

不是MVC


$.each(data, function (i, item) {
这样前面加一个
data=eval(data);

报错:缺少‘;’

#13


如何在jQuery或javascript处理接收到json数据。可以参考:
http://www.cnblogs.com/insus/p/3713459.html

#14


你可以在后台直接把table的字符串组合完成,然后发给前台显示就完事了嘛