2

I have done an ajax request in my code and it works good. After that I want to extract only the necessary info and re-post it to another script. Until now here is my code:

$.ajax({
        type: "POST",
                url: url,
                data: {xhr_id: xhr_id},
                success: function (jsondata) {

                    var product_data = [];

                      for (var i = 0; i <= 3; i++) {

                        //alert(jsondata.products[i].product_description.toSource());

                        product_data[i] = {};
                        product_data[i]["product" + i] = jsondata.products[i].product_description;

                        //alert(product_data[i]["product" + i].toSource());
                    }
                },
                dataType: "json"
        });

The problem is that both the alerts work fine, displaying the information I want. However, I get an error message of "Uncaught TypeError: Cannot read property 'product_description' of undefined" which breaks the script and prevents me from doing anything else. What am I doing wrong, any ideas?

4
  • 1
    jsondata.products[i].product_description - you've got a hardcoded loop length your data is obviously less than that length. Commented Nov 4, 2016 at 14:33
  • @Phylogenesis - OP is already doing that. The array is declared and added to inside the success. Commented Nov 4, 2016 at 14:36
  • @tymeJV True. I have removed the incorrect comment. Commented Nov 4, 2016 at 14:39
  • @tymeJV Oh Jesus, it wasn't the hardcoded loop (I was getting the same error even with dynamic counter), but the '=' operator... What a silly mistake. Thank you for pointing me to that direction. Commented Nov 4, 2016 at 14:40

2 Answers 2

2

'product_description' of undefined" what it means is that your are trying to access property on undefined variable. That implies "jsondata.products[i]" resulted in undefined value which have occured due to index out of range.How many records are returned in jsondata 3 or 4,check and adjust the condition in for loop

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

Comments

0

The parameter in the success() function of $.ajax is a string. You have to put it through a parse function to make json. See your code below modified but not tested.

$.ajax({
        type: "POST",
                url: url,
                data: {xhr_id: xhr_id},
                success: function (jsondata) {
                    var oData;
                    try { oData=jQuery.parseJSON(jsondata) }
                    catch(err) {
                        alert("Problem parsing json string : " + jsondata)
                        return false
                    }

                    var product_data = [];

                      for (var i = 0; i <= 3; i++) {

                        //alert(oData.products[i].product_description.toSource());

                        product_data[i] = {};
                        product_data[i]["product" + i] = oData.products[i].product_description;

                        //alert(product_data[i]["product" + i].toSource());
                    }
                },
                dataType: "json"
        });

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.