6

I'm getting this data from the database in the controller. I have used adslashes while saving and stropslashes while retrieving data. Please see controller and php file. I need to get this error fixed. I hope someone can tell what I'm doing wrong.

{"records":[{"id":"425","name":"a","content":"a\b<br>a<br>b"},{"id":"426","name":"1","content":"1\2"}]} 

Also please check.. ionicnotepad.com/www this is what im practicing making. If you save data with a break in it.. anugular no matter what is showing
in the textarea. I tried everything. $sce, ng-bind-html, ng-sanitize, even the directive if you use divs and conteneditable.

//controller
app.value('count', 0);

app.controller('retrieveController', ['$sce','noteRetrieve','$state','alterNote','count','$ionicModal','$scope', function($sce, noteRetrieve, $state, alterNote, count, $ionicModal, $scope){
  var controller = this;
  var temp = localStorage.getItem("ascOrDsc");

  if(temp == "asc" || temp == null){
    controller.orderValue = 'id';
    controller.buttonDown = false;
    controller.buttonUp = true;
    localStorage.setItem("ascOrDsc", "asc");
  } else {
    controller.orderValue = '-id';
    controller.buttonUp = false;
    controller.buttonDown = true;
  }


  noteRetrieve.all()
    .success(function(data){

      var newData = data.replace(/&lt;br&gt;/g, '<br>');
      newData = newData.substring(1, newData.length - 1);
      // newData = angular.fromJson(newData);
      console.log(newData);

      // controller.allSaved = newData.records;
      // for(i = 0; i < newData.records.length; i++){
      //   count++;
      // }
      // controller.noOFNotes = count;
  });



  controller.deleteNote = function(noteId, noteName, noteContent){

    var currentNote = {
      id: noteId,
      name : noteName,
      content : noteContent
    };

    controller.itemId = noteId;

    alterNote.delete(currentNote)
      .success(function(){
          $state.go('notes.allnotes', {}, {reload: true});
      });
    };



    controller.changOrder = function(){
      var ascOrDscValue = localStorage.getItem("ascOrDsc");
      if(ascOrDscValue == 'asc'){
        controller.orderValue = '-id';
        controller.buttonUp = false;
        controller.buttonDown = true;
        localStorage.setItem("ascOrDsc", "desc");
      } else {
        controller.orderValue = 'id';
        controller.buttonUp = true;
        controller.buttonDown = false;
        localStorage.setItem("ascOrDsc", "asc");
      }
     };

     $ionicModal.fromTemplateUrl('templates/createNote.html', {
       scope: $scope,
       animation: 'slide-in-up'

     }).then(function(modal){
       $scope.modal = modal;
     });

     $scope.createModal = function() {
       $scope.modal.show();
       localStorage.setItem('newNoteAvailable', 'no');
     };

     $scope.closeModal = function() {
       if(localStorage.getItem('newNoteAvailable') == 'yes'){
         $state.go('notes.allnotes', {}, {reload: true});
       } else {
         $state.go('notes.allnotes', {}, {reload: false});
       }
       $scope.modal.hide();
       $ionicModal.fromTemplateUrl('templates/createNote.html', {
         scope: $scope,
         animation: 'slide-in-up'

       }).then(function(modal){
         $scope.modal = modal;
       });
     };

}]);



//php

$sql = "SELECT *
        FROM noteFile";
$result = mysqli_query($conn, $sql);

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {
      $outp .= ",";
    }
    $outp .= '{"id":"'  . $rs["id"] . '",';
    $outp .= '"name":"'   . $rs["name"] . '",';
    $outp .= '"content":"'. $rs["data"] . '"}';
}

$outp ='{"records":['.$outp.']}';


$outp = json_encode($outp);

// var_dump($outp);

$outp = stripslashes($outp);


echo ($outp);
3

1 Answer 1

3

The part "content":"1\2" is not valid, it should be "content":"1\\2"

Sign up to request clarification or add additional context in comments.

5 Comments

im trying and trying nbut im not able to make it work.. can anybody tell me exactly what to do?
Don't use adslashes or any other function before you save the JSON to the database. Also do not use stropslashes while retirving data. Just save the json as sting and it should be fine. JSON encode handle the escaping: echo json_encode(['content' => '1\2']); //{"content":"1\\2"}
{"id":"462","name":"test","content":"a"}]} .. i entered a\b and \b have escaped
Error: JSON.parse: bad control character in string literal at line 1 column 406 of the JSON data . this is the error i get with this approach
while saving before post call in angular im doing toJson when i reach php file for saving im doing json decode.. and json_encode before sending data back from server and when i get data back in controller im doing fromJson.. I hope this all is in order...

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.