SyntaxError:意外的标记

时间:2021-10-11 22:30:03

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.

我使用php脚本(save-data.php)将数据成功保存到json文件中,但是我无法使用get-data.php脚本正确获取它。

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

错误消息:angular.js:12520语法错误:在Object.parse(本机)位置0的JSON中的意外标记<

save-data.php:

保存-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:

得到-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:

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

编辑:我不需要将json文件解码为json,也不需要解析它(所有注释都在脚本中)。

2 个解决方案

#1


11  

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.

总是有99.9999999%的时间你在错误中获得USON中的Unexpected token <作为位置0,你没有从服务器接收json。之后您收到了一条html错误消息,其中包含您的json。< p>

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

中的前导 <是错误的来源,因为它的位置为0(第一个字符)。< p>

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

检查从服务器返回的原始数据,并修复PHP正在吐出的错误/警告。

#2


2  

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

请检查您的PHP文件。因此可能存在不需要的回声,因此json响应无法得到确切的响应。我以这种方式解决了我的问题。希望这有用!!

#1


11  

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.

总是有99.9999999%的时间你在错误中获得USON中的Unexpected token <作为位置0,你没有从服务器接收json。之后您收到了一条html错误消息,其中包含您的json。< p>

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

中的前导 <是错误的来源,因为它的位置为0(第一个字符)。< p>

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

检查从服务器返回的原始数据,并修复PHP正在吐出的错误/警告。

#2


2  

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

请检查您的PHP文件。因此可能存在不需要的回声,因此json响应无法得到确切的响应。我以这种方式解决了我的问题。希望这有用!!