0

My ajax code looks like this

$.ajax({
    type : "POST",
    dataType : "JSON",
    url : "index.php",
    success : function(data){           
        for(i=0;i<data.length;i++)
        {
            console.log(data[i].1_assigned_accepted);
        }
    }
})

Now console.log(data[i].1_assigned_accepted); is giving me error, even though i have 1_assigned_accepted as column name in my table. jQuery doesn't consider 1_. How to resolve this?

11
  • 1
    check whether you are accessing it properly..can you give the response Commented Apr 28, 2016 at 9:30
  • 1
    What does your JSON look like? Commented Apr 28, 2016 at 9:30
  • 1
    can you post what index.php returns Commented Apr 28, 2016 at 9:31
  • 1
    You can't start a variable with a number, change it to assigned_accepted_1 and it should start working for you. Commented Apr 28, 2016 at 9:31
  • 1
    Parse it using JSON.parse() Commented Apr 28, 2016 at 9:31

3 Answers 3

1

Try:

console.log(data[i].["1_assigned_accepted"]);
Sign up to request clarification or add additional context in comments.

5 Comments

@RhysBradbury Actually, he does not need to include var i = 0. Try if yourself: for (i = 0; i < 10; i++) { console.log(i); }, the rest of your answer is "tips" he doesn't need to fix his code. My answer works.
console.log(data[i].["1_assigned_accepted"]);, you dont need the extra .. And just because it runs does NOT mean its acceptable JavaScript. Im pretty sure his request would return a HTTP error because contentType isnt defined.
They aren't just 'tips'. They are valid improvements and standard best practises.
@RhysBradbury That's something for the OP to decide to practice later-on. Although they're valid best practices and standards, solving the issue at hand instead of focusing on superfluous is more effecient
@RhysBradbury It is, and a simple answer to their problem will help them overcome the issue they're facing to continue growing as a developer and finding new issues to research or ask solutions to, however flooding them with information on standards they really don't need to follow to get the job done doesn't help them in this specific scenario. Like I said, the guidance you gave does give people a better idea of what standard code practices are out there, but this is more for their manager to follow up on in code-reviews and not for us to dictate. Some companies do things their way regardless
1

Try to use bracket form.

console.log(data[i]['1_assigned_accepted']);

1 Comment

Beat you by 9 seconds :P
-1

To fix your code:

$.ajax({
    type: "POST",
    dataType: "JSON",
    url: "index.php",
    success: function(data) {
        console.log(data); //test to see if we get something back
        for (var i = 0; i < data.length; i++) {
            console.log(data[i]['1_assigned_accepted']);
        }
    }
});

The difference OP and mine is assignment of i=0; -> var i = 0; and using brackets for getting the value.

Some Notes:

When doing a post you usually send data using the data: option. (remember to Json.stringify(input)). When posting you may need more options:

function postJson(url, data, successCallback, errorCallback) {
  $.ajax({
      url: url,
      type: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/json',
      dataType: 'json',
      success: successCallback,
      error: errorCallback
  });
}

Notice there's contentType, dataType and data being used here. You usually want all of these when doing a post.

Invoke like so:

var data = { name: 'Rhys' };
postJson(
  'index.php',
   data,
   function(responseData) {
     console.log(responseData);
   },
   function(err) {
     console.error(err);
   }
);

Another point:

You are assuming that your response object is an array, you could test using instanceof.

I hope this helps,

Rhys

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.