Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected token < in JSON at position 0 at Object.parse (native) (AngularJS)

I'm successfully saving my data into a json file with a php script (save-data.php) but I'm unable to fetch it correctly with my get-data.php script.

Error message: angular.js:12520 SyntaxError: Unexpected token < in JSON at position 0 at Object.parse (native)

save-data.php:

<?php

$json = file_get_contents("php://input");

$file = fopen('C:/test/save-data.json','w+');

fwrite($file, $json);

fclose($file);

?>

get-data.php:

<?php

//header('Content-Type: application/json');

$json = file_get_contents('C:/test/save-data.json');

//Decode JSON
//$json_data = json_decode($json, true);

//Print data
echo $json

?>

save-data.json:

{
  "id": "179",
  "var1": "variable1",
  "var2": "variable2"
}

sample controller:

// save data (myModel: id, var1, var2)
  $scope.save = function() {
    console.log('Creating a JSON');
    $scope.jsonString = angular.toJson($scope.myModel, true);
    $http.post('save-data.php', $scope.jsonString).then(function(data) {
      $scope.msg1 = 'Data saved';
    });
    $scope.msg2 = 'Data sent: '+ $scope.jsonString;
  };

// get data
  $scope.get = function() {
    $http.get('get-data.php').then(function(data) {
      //$scope.my_data = JSON.parse(data);
      console.log(data.data);
    });
  };

EDIT: I didn't need to decode the json file to json nor to parse it (all commented in scripts).

like image 766
Ariana Avatar asked Jun 21 '16 15:06

Ariana


People also ask

How do I fix unexpected token a JSON at position 0?

Re: Unexpected token in JSON at position 0 This usually means that an error has been returned and that's not valid JSON. Turn on Debugging and (after reproducing the error) check the web server error logs. Hopefully, the underlying error will land in one of those locations.

How do I fix an unexpected token?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What is meaning of O in JSON?

O is the first letter of the string. JSON.parse(object) compares with jQuery.parseJSON(object);

How do I debug unexpected end of JSON input?

You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.


2 Answers

Invariably, 99.9999999% of the time you get Unexpected token < in JSON as position 0 in the error, you did NOT receive json from the server. You received an HTML error message with your json following afterwards.

<p>PHP warning: blah blah blah</p>
{"foo":"bar"}

The leading < in the <p>... is where the error comes from, because that's position 0 (first character).

Check the raw data coming back from the server, and fix whatever the error/warning that PHP is spitting out.

like image 90
Marc B Avatar answered Oct 03 '22 16:10

Marc B


Please Check your PHP file. In that might be unwanted echo is present so the json response could not get the exact response. I resolve my issue in this way. Hope this is helpful!!

like image 39
Tushar Rmesh Saindane Avatar answered Oct 03 '22 16:10

Tushar Rmesh Saindane