0

My php code is as below

if ($data = $user->fetch()) 
{
 echo json_encode(array("output" => $data));
}
else
{
 echo json_encode(array("output" => $data));
}

My javascript code is as below

success: function(response) 
{ 
 alert(response); //shows me [object Object] 
 alert(response[0].output); // shows me nothing
 alert(response[0]); //shows me undefined
 alert(JSON.stringify(response)); //shows me {"output":false} 
 //I would like to get only true or false
}

From this post (jQuery AJAX call returns [object Object]) I knew that [object Object] is basically an array.

From this post (jQuery Ajax: get specific object value) I got below words.

"As you can see your response starts with [ and ends with ]. So your response is an array.

Then, inside the array, you get objects (starting with { and ending with }).

So your data is an array, which can be accessed with data[x] (x being the index), and each member of the selected object can be accessed with the .dot notation: .word, .text etc.

So in your case, if there's only one result, you can do data[0].word."

In this case I should get my expected result using alert(response[0].output);

But I am not getting that result. Can anyone help me in this regard ??

asked Nov 11, 2014 at 8:00
1
  • you will get it by response.output.Try this at your javascript. var data=response.output.Now data[0] is first element of your $data and others. Commented Nov 11, 2014 at 8:07

2 Answers 2

1

First off, make sure that you have explicitly set that you're going to receive JSON:

dataType: 'JSON',

Then try to access them thru this instead:

success: function(response) {
 console.log(response.output);
 // kindly open your console browser to see contents
 // i think this is F12
 if(response.output) {
 // do something if true
 } else {
 // do something if false
 }
}
answered Nov 11, 2014 at 8:01
Sign up to request clarification or add additional context in comments.

Comments

0

You can try also this one:

success: function(response) {
 // check your console
 console.log(response);
 //this two will give you same result
 console.log(response.output);
 console.log(response['output']);
 //either of this two
 var output = response.output; 
 var output = response['output']; 
 if(output){
 //code here
 }
}
answered Nov 11, 2014 at 8:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.