最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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).

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).

Share Improve this question edited Jun 21, 2016 at 20:05 Ariana asked Jun 21, 2016 at 15:46 ArianaAriana 5092 gold badges7 silver badges13 bronze badges 4
  • What happens if you go to get-data.php in browser. I can 100% guarantee that your php code is not executed and it returns file as is. – E_p Commented Jun 21, 2016 at 15:48
  • 2 Why you are calling json_decode() in your get-data.php at all? This will converts your JSON object into array so obviously angular cannot parse it after that (JSON.parse() expects JSON object). Just pass it without this conversion. – mitkosoft Commented Jun 21, 2016 at 15:51
  • without the conversion (json_decode()): angular.js:12520 SyntaxError: Unexpected token o in JSON at position 1 at Object.parse (native) – Ariana Commented Jun 21, 2016 at 15:56
  • sounds like your endpoint isn't returning json. – Kevin B Commented Jun 21, 2016 at 15:57
Add a comment  | 

2 Answers 2

Reset to default 12

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.

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

发布评论

评论列表(0)

  1. 暂无评论