0

I try to build registration form, I have problem when I get the errors back from the PHP to the javascript.

I got the right data but i got it like a string and I need it like a javascript object because it can contain some errors in the same time..

Here is the data that I got back form the server side:

{"success":false,"errors":{"name":"Name is required.","superheroAlias":"Superhero alias is required."}}

This data is a string, when i do data[0] i get "{" i need to get that data back as javascript object. I guess i supose to create function Json2Javascript or somthing like that but i dont know how its need to look, can you help me please create the function that will that?

If you write function please show me how to call the vars to, for example if i need to get the error on the name i want to get it like:

var a = data.errors.name;

Please help i tryed to find this function like hours =\ * I know how to create object in php and convert it to json and then to javascript object but i do that only with strings vars not on objects..

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rafael
  • 157
  • 3
  • 13

1 Answers1

2

You need to parse the string with:

var dataObject = JSON.parse(data);
bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • Thank you all, i triyed json parse but its not worked, affter alot of time i discover that the web hosting that i use add a script to the response data! and when they add the script the parse not worked well...=] – Rafael Oct 19 '15 at 05:39