JQuery AJAX 响应仅返回 [object Object] 且在 C# ASP.NET 中未定义

标签 jquery asp.net ajax json

我已经看到这个问题被问了很多次,但请尝试了解我的实际问题是什么。

<小时/>

我的 JavaScript 代码是:

     $.ajax({
            type: "POST",
            url: 'ScheduleCampiagn.aspx/GetTemplate',
            data: '{TempId: ' + $('#<%=ddl_Select_Template.ClientID%>').val() + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response);
            },
            failure: function (response) {

            }
        });

我在 ScheduleCampiagn.aspx.cs 中的 GetTemplate 函数是:

[System.Web.Services.WebMethod]
public static List<TemplateClass> GetTemplate(int TempId)
{
    List<TemplateClass> dataTemp = new List<TemplateClass>();
    TemplateClass temp = new TemplateClass();
    String cmd = "Select Tmpl_Id,Tmpl_Body_Content from TBL_DESIGN_TEMPLETE WHERE Tmpl_Id='" + TempId + "'";
    DataSet dsTemp = new DataSet();
    dsTemp.Clear();
    con.Retrive(cmd, ref dsTemp);
    cmd = string.Empty;
    temp.TempId = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["Tmpl_Id"]);
    temp.TempContent = Convert.ToString(dsTemp.Tables[0].Rows[0]["Tmpl_Body_Content"]);
    dataTemp.Add(temp);
    return dataTemp;
}

正如我所料,GetTemplate 函数仅返回一行。但我的问题是:

<小时/>

1.执行时显示警告框,内容为[object Object]。

2.当我将成功函数更改为 警报(响应[0].TempId); 它表明响应是不可防御的

3.i还使用FireBug调试js代码,它显示ReferenceError:response is undefiened。

4.i也尝试使用response.d来获取值,但它不起作用。

我只想获取 dataTemp i:e 的内容

<小时/>
        1.dataTemp.TempId

        2.dataTemp.TempContent

请帮助我解决这个问题,或者请让我知道我在这些代码中错过了什么,我已经通过搜索损失了一整天的时间。

非常感谢

最佳答案

响应被包装在 d 属性内。

$.ajax({
            type: "POST",
            url: 'ScheduleCampiagn.aspx/GetTemplate',
            data: '{TempId: ' + $('#<%=ddl_Select_Template.ClientID%>').val() + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                // fail-safe for older ASP.NET frameworks
                var data = response.hasOwnProperty("d") ? response.d : response;
                alert(data.TempId);  //Changed here
            },
            failure: function (response) {

            }
        });

代码隐藏方法。

//Changed the return type to a single object instead of list.
[System.Web.Services.WebMethod]
public static TemplateClass GetTemplate(int TempId)
{

    TemplateClass temp = new TemplateClass();
    String cmd = "Select Tmpl_Id,Tmpl_Body_Content from TBL_DESIGN_TEMPLETE WHERE Tmpl_Id='" + TempId + "'";
    DataSet dsTemp = new DataSet();
    dsTemp.Clear();
    con.Retrive(cmd, ref dsTemp);
    cmd = string.Empty;
    temp.TempId = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["Tmpl_Id"]);
    temp.TempContent = Convert.ToString(dsTemp.Tables[0].Rows[0]["Tmpl_Body_Content"]);

    return temp;
}

关于JQuery AJAX 响应仅返回 [object Object] 且在 C# ASP.NET 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051676/

相关文章:

php - 网页扫描前隐藏提交按钮

c# - SQL - ASP.NET Core 2.1.7 中的故意注入(inject)

带有 StructureMap 的 Asp.Net MVC 5 自定义 Action 过滤器

c# - 优化在asp.net mvc 4中将大数据集加载到表中

jquery - 嵌套jquery的.append()、.each()、append()并保留第一个 "this"引用

javascript - 如何禁用 Bootstrap 表单字段?

javascript - 是否可以在未连接互联网时继续使用 Web 应用程序?

ios - 如何监控 http 请求以便在我的 ios 应用程序中看到我的 header 请求

javascript - jQuery动画仅当ajax信息不相同时

php - 使用 jQuery 的动态链式选择框