1

In my AngularJS application, when I trying to get data with new line from database, I get the following error :

 SyntaxError: Unexpected token 
at Object.parse (native)
at Object.oc [as fromJson] (http://localhost:8080/pjib/js/angular.min.js:14:156)
at new <anonymous> (http://localhost:8080/pjib/admin_pbjib/news/index.php:191:34)
at Object.e [as invoke] (http://localhost:8080/pjib/js/angular.min.js:37:96)
at $get.z.instance (http://localhost:8080/pjib/js/angular.min.js:76:210)
at http://localhost:8080/pjib/js/angular.min.js:59:164
at s (http://localhost:8080/pjib/js/angular.min.js:7:408)
at v (http://localhost:8080/pjib/js/angular.min.js:59:148)
at g (http://localhost:8080/pjib/js/angular.min.js:52:9)
at g (http://localhost:8080/pjib/js/angular.min.js:52:26)

My code (php):

 $getData = json_encode(GetAllDataFromServer($table_name));

JSON encoded output string :

 [  
   {  
      "news_id":"1",
      "heading":"In purto percipit ......t",
      "news_body":"In purto percipit nam, .........",
      "news_img":"",
      "create_date":"2015-02-21",
      "display_status":"Yes"
  },
  {  
      "news_id":"2",
      "heading":"Habemus adipisci ....",
       "news_body":"In .... some string .... reque choro.\r\n \r\nHabemus adipisci .... some string .... dissentiunt.\r\n \r\nVel aliquid .... some string ....  disputando te cum.\r\n \r\nIn usu .......",
      "news_img":"",
      "create_date":"2015-02-21",
      "display_status":"No"
   }
]

My AngularJS code :

 // only problem when '\r\n' appear in the json object(table column)
 $scope.news = angular.fromJson('<?=$getData ?>');  

Thanks in advance!!!

1 Answer 1

1

I solve my problem using the following code :

php code :

$getData = json_encode(GetAllDataFromServer($table_name));

After encoding db info into json fotmat, just do the following code.

AngularJS/JavaScript Code :

scope.news = <?=$getData ?>;
Sign up to request clarification or add additional context in comments.

1 Comment

In other words, you do not need to parse it twice.

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.