hijinxbassist
hijinxbassist

Reputation: 4591

jquery ajax not returning data on success c# asp.net

I cant for the life of me figure out why my data being returned is empty. In fiddler i see the json

d=[{"response":[{"h":"h1"},{"h":"h1"}] }]

in fiddler there is a 200 status on the row where i see the json, but no other rows after that one ( maybe its not returning? ). This is the code i am using

$('.SomeLink').click(function () {
    var sfn = $('#ID1').val();
    var sfp = $('#ID2').val();
    var sfi = $('#ID3').val();
    var gid = $('#ID4').val();
    $.ajax({
        type: "POST",
        cache: false,
        url: '/AjaxHandler.aspx/GetNewHtml',
        contentType: "application/json; charset=utf-8",
        data: "{'a':'" + sfn + "','b':'" + sfp + "','c':'" + gid + "','d':'" + sfi + "'}",
        dataType: "json",
        success: function (data) {
            alert(data.response[0].h); //acts like a syntax error/no alert box
            alert(data); // [object Object]
            alert(data.response); // undefined
            alert(data.response.count); //acts like a syntax error/no alert box
        },
        error: function (e) {
            alert("Error: " + e.responseText);
        }
    });
});

AjaxHandler.aspx

[System.Web.Services.WebMethod()]
public static string GetNewHtml(string a, string b, string c, string d)
{
    List<Samp> samp = new List<Samp>()
    {
        new Samp{h = "h1"},
        new Samp{h = "h1"}
    };
    return Serialize(new { response = samp });
}
private static string Serialize(object obj)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(obj);
}

Samp Class

public class Samp
{
    public string h = "";
}

This is my first time using jquery ajax with asp.net so im sure im missing something that is probably relatively simple. Im using .Net 4.0 , jquery 1.7.1 , iis 7.5

Upvotes: 0

Views: 3304

Answers (2)

bluetoft
bluetoft

Reputation: 5443

its tripping over the quotes around the property names. try string indexes. try

data["response"][0]["h"]

returns h1

Upvotes: 0

AaronS
AaronS

Reputation: 7703

Try data.d for your return object:

alert(data.d);

Upvotes: 3

Related Questions