0

I have this javascript code :

$.post(url, { 'ids[]': row_ids },function(response) 

for information post execute this :

echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_deleted')));

When I debug I found that the response contains this :

"Array(    [0] => 4555){"success":true,"message":"Vous avez supprim\u00e9 avec succ\u00e8s"}"

I just want to test if success equal true but when I do it like this : if(response.success === true) it doesn't work.

I just wanna know to accede to values of response.

  • did you try in your encode line to put true in apostrophes as well? – rudtek Jul 31 '17 at 21:22
  • yes i did but it doesn t work – marouane Jul 31 '17 at 22:05
  • check typeof response to see if really is a json object – Cesar Henrique Damascena Aug 1 '17 at 0:52
  • Have you tried using response[0].success instead? – Jack Johansson Aug 1 '17 at 1:25
0

If you want to check the value of your JSON response in JS, you need to parse the JSON object you're returning before trying to access any values:

jsonResponse = JSON.parse(response);
if (jsonResponse.success === true) { /*do something*/ }
  • i did it but i had error while JSON.parse(response); i think response isn't on right json format – marouane Aug 1 '17 at 0:33
  • Actually, what is the error that's coming up when you try to JSON.parse ? You'll definitely need to parse it before accessing it in JS – giolliano sulit Aug 1 '17 at 0:40
  • Actually it looks like your function is returning an 'echo' of the data rather than the actual data. Change your PHP function to 'return json_encode' instead of 'echo json_encode' – giolliano sulit Aug 1 '17 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.