0

This is code I'm using right now to do AJAX via JQuery:

       $.ajax({
            type: "POST",
            url: linktopage,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $.each(data, function (index, item) {
                    alert(item.productId);
                });
            },
            error: function (xhr, status, error) {

            }
        });

where index = d and item = "[{"productId":5284598},{"productId":5790923}]" but alert(item.productId) is undefined, how can i access each productId?

4
  • 1
    It would be much easier if you could give us a sample of the returned JSON data. Commented Jan 17, 2013 at 19:46
  • this is the returned JSON data: "[{"productId":5284598},{"productId":5790923}]" Commented Jan 17, 2013 at 19:47
  • 1
    In which case what you have should work: jsfiddle.net/R8sQp Commented Jan 17, 2013 at 19:49
  • If you console.log(data) and it is a string instead of an object, then you are not passing the data back from the server correctly. Commented Jan 17, 2013 at 19:54

2 Answers 2

1

In your example, there is an array of items. You must iterate that to get to the productId.

for( var i = 0; i < item.length; i++ ){
 console.log(item[i].productId);
}

edit

It is possible that your data is still a string, in which case, you may want to use

data = JSON.parse(data);

before iterating. (more on parse: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse )

like this (you could also test to see if it were a string first):

$.ajax({
        type: "POST",
        url: linktopage,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if( typeof data == "string" )data = JSON.parse(data);
            $.each(data, function (index, item) {
                alert(item.productId);
            });
        },
        error: function (xhr, status, error) {

        }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right Travis the received JSON was a string and I need to Parse it before I can use it. Thanks for the help! :)
1

The data is a string entry in your case. So you have to parse it to a JSON first. Update you code to this

$.ajax({
            type: "POST",
            url: linktopage,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var dta = $.parseJSON(data);
                $.each(dta , function (index, item) {
                    alert(item.productId);
                });
            },
            error: function (xhr, status, error) {

            }
        });

Comments

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.