3

I am using web forms and I have a webmethod in the code behind that my ajax function is calling. It appears that the webmethod is returning data but I am having trouble parsing the returned json string into table rows

Here is my code behind method (Web Method):

public class Part
{
    public string Number { get; set; }
    public string Price { get; set; }
    public string NetAvailable { get; set; }
    public string Location { get; set; }
    public string Distance { get; set; }
    public string Phone { get; set; }
}

[WebMethod]
public static List<Part> GetParts(string partnumber)
{
    List<Part> parts = new List<Part>();

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NXT"].ToString()))
    {
        con.Open();
        using (SqlCommand command = new SqlCommand("SELECT prod, qtyonhand, whse, listprice FROM icsw where prod LIKE @partnumber", con))
        {
            command.Parameters.AddWithValue("@partnumber", "%" + partnumber + "%");

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Part part = new Part
                    {
                        Number = reader[0].ToString(),
                        Price = reader[3].ToString(),
                        NetAvailable = reader[1].ToString(),
                        Distance = "0.0",
                        Location = reader[2].ToString(),
                        Phone = GetBranchPhoneNumber(reader[2].ToString())
                    };
                    parts.Add(part);

                }
            }
        }
    }

    return parts;
}

Here is my AJAX call:

$("#searchbtn").click(function() {
    $.ajax({
        url: 'partslocator.aspx/GetParts',
        type: 'POST',
        data: JSON.stringify({ 'partnumber': $("#searchtbx").val()}),
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {

            for (var i in data) {
                alert(JSON.stringify(data[i]));
                $('#searchresultstble').append("<tr><td>" + data[i].Number + "</td><td>" + data[i].Price + "</td><td>" + data[i].NetAvailable + "</td><td>" + data[i].Location + "</td><td>" + data[i].Distance + "</td><td>" + data[i].Phone + "</td></tr>");
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});

Here is what data is coming back as:

{"d":[{"__type":"partslocator+Part","Number":"0-2809 1025","Price":"2.50000","NetAvailable":"0.00","Location":"82pk","Distance":"0.0","Phone":"test"},{"__type":"partslocator+Part","Number":"0-2809 1025","Price":"2.58000","NetAvailable":"0.00","Location":"Phx","Distance":"0.0","Phone":"test"}]}

Why am I getting undefined for all the rows that are being added?

1 Answer 1

3

You'll note that the returned data is not actually an array, but an object with a value (d) that contains that array. Try this:

    success: function (returnedData) {
        var data = returnedData.d;
        for (var i in data) {
            alert(JSON.stringify(data[i]));
            $('#searchresultstble').append("<tr><td>" + data[i].Number + "</td><td>" + data[i].Price + "</td><td>" + data[i].NetAvailable + "</td><td>" + data[i].Location + "</td><td>" + data[i].Distance + "</td><td>" + data[i].Phone + "</td></tr>");
        }
    },
Sign up to request clarification or add additional context in comments.

2 Comments

how for loop work without increment please explain me your ans is worng
@darshandave: This is JavaScript's for...in loop.

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.