SyntaxError:Object.parse AngularJS上的意外标记

时间:2015-02-20 20:10:31

标签: angularjs

在我的 AngularJS 应用程序中,当我尝试从数据库获取数据时使用新行时,出现以下错误:

 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)

我的代码(php)

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

JSON编码输出字符串:

 [  
   {  
      "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"
   }
]

我的AngularJS代码:

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

提前致谢!!!

1 个答案:

答案 0 :(得分:1)

我使用以下代码解决了我的问题:

php代码:

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

将db info编码为json fotmat后,只需执行以下代码即可。

AngularJS / JavaScript代码:

scope.news = <?=$getData ?>;