【问题标题】:JQuery AJAX response returning only [object Object] and Undefined in C# ASP.NETJQuery AJAX 响应仅返回 [object Object] 和 C# ASP.NET 中的未定义
【发布时间】:2015-05-05 11:24:41
【问题描述】:

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


我的 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) {

            }
        });

而我在 ScheduleCampiag.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.我也用FireBug调试js代码显示ReferenceError: response is undefiened。

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

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


        1.dataTemp.TempId

        2.dataTemp.TempContent

请帮助我,或者让我知道我在这些代码中遗漏了什么,我已经通过搜索失去了一整天。

非常感谢

【问题讨论】:

  • 既然你有firebug,你可以使用console.log而不是alert来查看实际对象包含的内容——然后在firebug控制台中你可以看到该对象。也可以在网络选项卡中查看对象>找到ajax调用>查看响应
  • 我认为您需要从您的 webmethod 返回类型为 json 的响应,因为您的 ajax 期望相同...
  • 是的,我收到一个错误,即 ReferenceError: response is undefiened
  • 你可以在调试窗口中看到实际的对象(F12 for chrome),在警报前放一个断点。
  • 尝试警报(response.d.[0].TempId);

标签: jquery asp.net ajax json


【解决方案1】:

响应包含在 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;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
相关资源
最近更新 更多