Reputation: 2422
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?
Upvotes: 0
Views: 71
Reputation: 1707
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
Upvotes: -1
Reputation: 1054
Try to use bracket form.
console.log(data[i]['1_assigned_accepted']);
Upvotes: 1