Tufan Chand
Tufan Chand

Reputation: 752

JQuery AJAX response returning only [object Object] and Undefined in C# ASP.NET

I have seen this question has been asked so many times, but please try to understand what is my actual problem.


My JavaScript codes are :

     $.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) {

            }
        });

And my GetTemplate function in ScheduleCampiagn.aspx.cs is:

[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;
}

The GetTemplate function returns only one row as I Expected. But my problems are:


1.when it is executing alert box shown with content as [object Object].

2.when i change the success function as alert(response[0].TempId); it shows that response is undefiend

3.i also debug the js code with FireBug It is showing ReferenceError: response is undefiened.

4.i also tried with response.d to get the value, but it is not working.

I only want to fetch the content of dataTemp i:e


        1.dataTemp.TempId

        2.dataTemp.TempContent

Please help me on this or kindly let me know what i have missed in these code, I have already lost 1 whole day by searching it.

Thank you very much

Upvotes: 4

Views: 5123

Answers (1)

sudhansu63
sudhansu63

Reputation: 6180

Response is wrapped inside d property.

$.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) {

            }
        });

Code behind method.

//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;
}

Upvotes: 3

Related Questions