4

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

12
  • Since you have firebug you can use console.log instead of alert to see what the actual object contains - then in the firebug console you can see the object. You can also see the object in the network tab > find the ajax call > look at the response Commented May 5, 2015 at 11:26
  • I think you need to return response of type json from your webmethod as you ajax is expecting the same... Commented May 5, 2015 at 11:28
  • yes i got an error that is ReferenceError: response is undefiened Commented May 5, 2015 at 11:29
  • you can see the actual object in debug window (F12 for chrome), put a break point vbefore alert. Commented May 5, 2015 at 11:29
  • 2
    try alert(response.d.[0].TempId); Commented May 5, 2015 at 11:29

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

i didnt see the code-behind change. I think he want to return list anyway
yes it is working nicely, thank you so much, but how i did not understand it properly
Check this link also given by @GuruPrasad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.