Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!!!

share|improve this question

2 Answers 2

You need to find a way, server-wise, to escape your newline fragments, like so:

"news_body":"In .... some string .... reque choro.\\r\\n \\r\\"

Depending on your server language (php?) you could use a string function, or regexp.

See this SO post for more on JSON spec: control characters in JSON

But it should also work by double-encoding with json_encode:

$scope.news = angular.fromJson('<?php echo json_encode($getData); ?>');

Applying twice the json_encode function should escape the control characters.

share|improve this answer
    
thanks a lot, I am working on it –  sabbir Feb 20 at 21:39
    
did the double json_encode do the trick, or did you have to use a php string function to make valid json? –  Manube Feb 20 at 22:02
up vote 0 down vote accepted

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 ?>;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.