1

I'm pretty new to AngularJS. What I'm trying to do is to get data from a PHP file that will make requests on a DB and then will display them in a JSON format. For now I'm just testing to see if I can get the data from the PHP and is not working. It displays nothing. In console I get this message:

SyntaxError: Unexpected token {
             at Object.parse (native)
             at cc (http://localhost/SiteKardinal/includes/angular.min.js:14:360)
             at Ud.e.defaults.transformResponse (http://localhost/SiteKardinal/includes/angular.min.js:69:261)
             at http://localhost/SiteKardinal/includes/angular.min.js:69:22
             at r (http://localhost/SiteKardinal/includes/angular.min.js:7:290)
             at xc (http://localhost/SiteKardinal/includes/angular.min.js:69:4)
             at b (http://localhost/SiteKardinal/includes/angular.min.js:70:238)
             at F (http://localhost/SiteKardinal/includes/angular.min.js:100:187)
             at http://localhost/SiteKardinal/includes/angular.min.js:101:350
             at k.$eval (http://localhost/SiteKardinal/includes/angular.min.js:112:68) angular.min.js:92
GET http://localhost/SiteKardinal/includes/angular.min.js.map 404 (Not Found) angular.min.js.map:1

My code is this:

3
  • use json_encode for json encoding.. you forgot about commas.. Commented Nov 21, 2014 at 19:16
  • Thank you! I will try with the json_encode too. Commented Nov 21, 2014 at 20:40
  • It is very simple and always return correct json with unicode and quotes escaping - I write a snippet in the answers. Commented Nov 22, 2014 at 6:24

2 Answers 2

1

You miss comma in the JSON you are returning from your PHP script. this

$out .= '{"name":"name1","ocupatie":"student"}';
$out .= '{"name":"name2","ocupatie":"CEO"}';
$out .= '{"name":"name3","ocupatie":"student"}';

change to this

$out .= '{"name":"name1","ocupatie":"student"},';
$out .= '{"name":"name2","ocupatie":"CEO"},';
$out .= '{"name":"name3","ocupatie":"student"}';
Sign up to request clarification or add additional context in comments.

Comments

0

I know, that previous answer works,
but anyway - change your code to json_encode using,
because correct manual encoding is hard to implement
and cause errors very often..

$data = array(
  array('name' => 'name1', 'ocupatie' => 'ocupatie1'),
  array('name' => 'name2', 'ocupatie' => 'ocupatie2'),
  array('name' => 'name3', 'ocupatie' => 'ocupatie3'),
);

echo json_encode($data);

1 Comment

Great! I did it this way and it's just fine. Thank you!

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.