Getting undefined in JSON response
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using the CodeIgniter shopping cart. I am fetching the all the add to cart information and I am getting the output in the alert(data) but I am not able to check the o.qty
. I am getting undefined.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj, function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
Controller
public function primaryCartload()
{
$output=;
$count = 0;
foreach($this->cart->contents() as $items)
{
$count++;
$output = array(
'id' =>$items["id"],
'qty' =>$items["qty"],
'subtotal'=>$items["subtotal"],
'removebtn'=>$items["rowid"],
'cart_total'=>$this->cart->total()
);
}
$outputStore['outputStore']=$output;
if($count == 0)
{
$outputStore ['outputStore']= 0;
}
echo json_encode($outputStore);
exit();
}
I am getting the output in the alert(data)
{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}
but when I am accessing the o.qty then I am getting undefined
php json ajax codeigniter-3
|
show 7 more comments
I am using the CodeIgniter shopping cart. I am fetching the all the add to cart information and I am getting the output in the alert(data) but I am not able to check the o.qty
. I am getting undefined.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj, function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
Controller
public function primaryCartload()
{
$output=;
$count = 0;
foreach($this->cart->contents() as $items)
{
$count++;
$output = array(
'id' =>$items["id"],
'qty' =>$items["qty"],
'subtotal'=>$items["subtotal"],
'removebtn'=>$items["rowid"],
'cart_total'=>$this->cart->total()
);
}
$outputStore['outputStore']=$output;
if($count == 0)
{
$outputStore ['outputStore']= 0;
}
echo json_encode($outputStore);
exit();
}
I am getting the output in the alert(data)
{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}
but when I am accessing the o.qty then I am getting undefined
php json ajax codeigniter-3
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
tryconsole.log( typeof(o) );
before alert( . This is how you will know weathero
is an object or not .
– Tamim
Jan 4 at 7:55
1
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
tryalert(typeof(o))
– Tamim
Jan 4 at 7:57
|
show 7 more comments
I am using the CodeIgniter shopping cart. I am fetching the all the add to cart information and I am getting the output in the alert(data) but I am not able to check the o.qty
. I am getting undefined.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj, function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
Controller
public function primaryCartload()
{
$output=;
$count = 0;
foreach($this->cart->contents() as $items)
{
$count++;
$output = array(
'id' =>$items["id"],
'qty' =>$items["qty"],
'subtotal'=>$items["subtotal"],
'removebtn'=>$items["rowid"],
'cart_total'=>$this->cart->total()
);
}
$outputStore['outputStore']=$output;
if($count == 0)
{
$outputStore ['outputStore']= 0;
}
echo json_encode($outputStore);
exit();
}
I am getting the output in the alert(data)
{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}
but when I am accessing the o.qty then I am getting undefined
php json ajax codeigniter-3
I am using the CodeIgniter shopping cart. I am fetching the all the add to cart information and I am getting the output in the alert(data) but I am not able to check the o.qty
. I am getting undefined.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj, function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
Controller
public function primaryCartload()
{
$output=;
$count = 0;
foreach($this->cart->contents() as $items)
{
$count++;
$output = array(
'id' =>$items["id"],
'qty' =>$items["qty"],
'subtotal'=>$items["subtotal"],
'removebtn'=>$items["rowid"],
'cart_total'=>$this->cart->total()
);
}
$outputStore['outputStore']=$output;
if($count == 0)
{
$outputStore ['outputStore']= 0;
}
echo json_encode($outputStore);
exit();
}
I am getting the output in the alert(data)
{"outputStore":[{"id":"1","qty":1,"subtotal":5000,"removebtn":"c4ca4238a0b923820dcc509a6f75849b","cart_total":6000},{"id":"2","qty":1,"subtotal":1000,"removebtn":"c81e728d9d4c2f636f067f89cc14862c","cart_total":6000}]}
but when I am accessing the o.qty then I am getting undefined
php json ajax codeigniter-3
php json ajax codeigniter-3
asked Jan 4 at 7:51
user9437856user9437856
471314
471314
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
tryconsole.log( typeof(o) );
before alert( . This is how you will know weathero
is an object or not .
– Tamim
Jan 4 at 7:55
1
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
tryalert(typeof(o))
– Tamim
Jan 4 at 7:57
|
show 7 more comments
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
tryconsole.log( typeof(o) );
before alert( . This is how you will know weathero
is an object or not .
– Tamim
Jan 4 at 7:55
1
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
tryalert(typeof(o))
– Tamim
Jan 4 at 7:57
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
try
console.log( typeof(o) );
before alert( . This is how you will know weather o
is an object or not .– Tamim
Jan 4 at 7:55
try
console.log( typeof(o) );
before alert( . This is how you will know weather o
is an object or not .– Tamim
Jan 4 at 7:55
1
1
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
try
alert(typeof(o))
– Tamim
Jan 4 at 7:57
try
alert(typeof(o))
– Tamim
Jan 4 at 7:57
|
show 7 more comments
1 Answer
1
active
oldest
votes
You should take the array from data object.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj["outputStore"], function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.
– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034979%2fgetting-undefined-in-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should take the array from data object.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj["outputStore"], function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.
– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
add a comment |
You should take the array from data object.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj["outputStore"], function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.
– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
add a comment |
You should take the array from data object.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj["outputStore"], function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
You should take the array from data object.
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data) {
//alert(data);
if (data != 0) {
console.log(data);
alert(data);
var obj = JSON.parse(data);
$.each(obj["outputStore"], function(i, o) {
alert(o.qty);
if (o.qty != 0) {
$('#subtotal_details').html('Total cost:' + o.subtotal);
//alert('not empty');
});
}
else {
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('Total items:0');
}
}
});
});
answered Jan 4 at 8:00
TamimTamim
320511
320511
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.
– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
add a comment |
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.
– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
I am getting the error in console JSON.parse: unexpected character at line 1 column 1 of the JSON data
– user9437856
Jan 4 at 8:03
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.– Tamim
Jan 4 at 8:05
JSON.parse: unexpected character at line 1 column 1 of the JSON data
That means something wrong with returned data from backend.– Tamim
Jan 4 at 8:05
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
If the data shown in the original post is already an object then you don't need to call JSON.parse(data), just remove that and iterate through data["outputStore"]
– SPlatten
Jan 4 at 8:10
Give me some time to check
– user9437856
Jan 4 at 8:15
Give me some time to check
– user9437856
Jan 4 at 8:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034979%2fgetting-undefined-in-json-response%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try replacing o.qty != 0 with typeof o.qty == "number"
– SPlatten
Jan 4 at 7:54
try
console.log( typeof(o) );
before alert( . This is how you will know weathero
is an object or not .– Tamim
Jan 4 at 7:55
1
Also, I think your reference is wrong, based on your output shouldn't it be, o["outputStore"][index]["qty"] ?
– SPlatten
Jan 4 at 7:56
@Tamim, I am getting me undefined. I tried alert(console.log( typeof(o) ));
– user9437856
Jan 4 at 7:56
try
alert(typeof(o))
– Tamim
Jan 4 at 7:57