Reputation: 2025
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!!!
Upvotes: 1
Views: 7720
Reputation: 2025
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 ?>;
Upvotes: 1