1

I have an ajax call that fires a php script that returns an array.

the php:

 $errors[1] = "you didn't enter name"; 
 $errors[2] = "your email is incorrect";
 $errors[3] = "You didnt enter password";
 echo json_encode($errors);

the javascript:

 .....
 datatype:'json',
 success: function(result)
 {
 alert(result);   
 }

I would expect to see:

 {"1":"you didn't enter name","2":"your email is incorrect","3":"You didnt enter password"}

instead i see: ["you didn't enter name","your email is incorrect","You didnt enter password]

it seems like the json_encode did something funky. what am i missing

1
  • 1
    Because what you wanted was $errors['1'] and what you have is $errors[1]. Commented May 10, 2013 at 18:09

3 Answers 3

1

Try this:

echo json_encode($errors, JSON_FORCE_OBJECT);

http://www.php.net/manual/en/json.constants.php

Also, yout PHP script should contain:

header("Content-Type: application/json");

before any echo statements.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks @wroniasty, this seemed to help. how i have:<br/> <br/> {"0":"you didn't enter name","1":"your email is incorrect","2":"You didnt enter password"}<br/> <br> if i try to ititerate through it: for (i=0;i<array.length;i++)<br> when i print array[i]<br> (array[0] = {<br> array[1] = "<br> array[2] = 0<br> i get each individual character, not what is in "0"....make sense?<br>
To iterate over an Object use: for (var key in obj) { var element = obj[key]; console.log(element); }
I know i am noobing this all up. i get same result with the for loop. the console log shows { then " then 0 then " and so on...its almost like the javascript doesnt see it as an array but a long string.
Perhaps it IS a string, what does console.log(typeof result) output?
string....if i alert(result) i get this {"0":"firstName must contain a value.", "1":"lastName must contain a value.", "2":"callIdfirstName must contain a value.", "3":"callIdlastName must contain a value.", "4":"street must contain a value.", "5":"city must contain a value."} my code on the php page. echo json_encode($errors, JSON_FORCE_OBJECT); I even tried php::json_last_error() and got a 0.
|
1

Since you just used numbers as keys, it assumed you wanted an Array.

Comments

1

Then setting this wrong, because that way will fail, by undefined vars.

$errors[1] = "you didn't enter name"; 
$errors[2] = "your email is incorrect";
$errors[3] = "You didnt enter password";
echo json_encode($errors);

Don't use this array configuration, instead use:

$errors=array(NULL,
  "you didn't enter name",
  "your email is incorrect",
  "You didnt enter password"
);
echo json_encode($errors, JSON_FORCE_OBJECT);

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.