0

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

6
  • 1
    Have you tried inspecting the object in chrome or firebug and seeing what is actually inside of the data object? Commented May 2, 2013 at 5:29
  • Try using FireFox (with FireBug) or Chrome and using console.log(data) in your success handler to push the object out to the debug console, which will give you a better idea of how it comes out. Commented May 2, 2013 at 5:30
  • in your success: function (data) { do a console.log(data); and see whats in your console. Commented May 2, 2013 at 5:32
  • in the firebug console under json i see this "{"response":[{"h":"h1"},{"h":"h1"}]}" Commented May 2, 2013 at 5:34
  • Strong inclination says that data is actually your list ... but just a guess at this point ... and certainly not what i would expect since your serialized an anonymous object containing the list named response. Commented May 2, 2013 at 5:34

2 Answers 2

3

Try data.d for your return object:

alert(data.d);
Sign up to request clarification or add additional context in comments.

7 Comments

ooooh I can't remember but it was a long time ago in asp.net where I was taught to grab the data because jQuery stored it by default in data.d ... but it remains to be seen at this point.
yeah, it got me a few times :)
Theres my data!!! Thank you thankyou, thats something i will not forgot, for a while at least ;)
@hijinxbassist You should still learn to use the developer tools - FireBug in Firefox, or the integrated ones in Chrome - to troubleshoot things like this. FireBug and Chrome will both show a lot of information about the network traffic, and both support console.log() to push objects to the console.
@AaronS Great memory recall. Is that a behavior specific to previous versions of jquery libraries? Or is it by default, what ASP.Net web methods serialize to. Be curious to know who is the culprit.
|
0

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

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

returns h1

4 Comments

Writing data["response"] in JavaScript is effectively identical to writing data.response.
just tested it, acts like a syntax error with no alert box. The data.d worked.
ok sounds good. I was going off of his comment where his results were {"response":[{"h":"h1"},{"h":"h1"}]}... JSON.parse(thatString) worked for me...
yes that actually works beautify after parsing the result of data.d Excellent addition to the answer +1, not sure where that -1 came from..

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.