【问题标题】:SyntaxError: Unexpected token at Object.parse AngularJSSyntaxError:Object.parse AngularJS 中的意外标记
【发布时间】:2015-02-20 20:10:30
【问题描述】:

在我的 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 ?>');  

提前致谢!!!

【问题讨论】:

    标签: angularjs


    【解决方案1】:

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

    php 代码:

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

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

    AngularJS/JavaScript 代码:

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

    【讨论】:

    • 也就是说,你不需要解析两次。
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多