SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) - AngularJS

SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) - AngularJS

我很确定我传递给 register() 的所有参数都有值。这是 js 代码。

$scope.register = function(
    $reg_code, $prov_code, $citymun_code, $brgy_code, $street,
    $user_name, $user_email, $user_contact, $user_password
) {
    //displays the arguments 
    alert("region: " + $reg_code + ", province: " + $prov_code + ", citymun: " + $citymun_code   +  ", barangay: "
  + $brgy_code + ", street: " + $street + ", user_name: " + $user_name + ", user_email: " + $user_email + ", user_contact: "
  + $user_contact + ", user_password: " + $user_password);

    $http({
      method: "POST",
      url: "http://" + host + "/mobile/register.php",
      data: JSON.stringify({
          user_password_reg: $user_password,
          user_email_reg: $user_email,
          user_contact_reg: $user_contact,
          user_name_reg: $user_name,
          region: $reg_code,
          province: $prov_code,
          citymun: $citymun_code,
          barangay: $brgy_code,
          street: $street,
          echo: "1",
          success: "0",
          user_acc_type: "log account"
      }),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function(res) {
          alert("success1: " + res.data.success1 + ", success2: " + res.data.success2 + ", success3: " + res.data.success3);
    });


}

这是php脚本

<?php 
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json; charset=utf-8');

    //converts data to post
    $postData = file_get_contents("php://input");
    $post = json_decode($postData);


    $echo = array();
    $echo["success1"] = 0;
    $echo["success2"] = 0;
    $echo["success3"] = 0;

    if(isset($post["region"]) &&
        isset($post["province"]) &&
        isset($post["citymun"]) &&
        isset($post["barangay"]) &&
        isset($post["street"]) &&
        isset($post["user_password_reg"]) &&
        isset($post["user_name_reg"]) &&
        isset($post["user_email_reg"]) &&
        isset($post["user_contact_reg"])
    ) {
        $echo["success1"] = 1;
        $echo["success2"] = 1;
        $echo["success3"] = 1;

    } else {

        $echo["success1"] = 2;
        $echo["success2"] = 2;
        $echo["success3"] = 2;
    }

    echo json_encode($echo);
 ?>

这个程序还没有完成。我首先想确保 php 从 post 和 returns 或 echo 获取所有正确的参数是一个正确的值。但是,在这些代码中,我在控制台中收到此错误。

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at fromJson (ionic.bundle.js:9892)
    at defaultHttpResponseTransform (ionic.bundle.js:17406)
    at ionic.bundle.js:17491
    at forEach (ionic.bundle.js:9150)
    at transformData (ionic.bundle.js:17490)
    at transformResponse (ionic.bundle.js:18216)
    at processQueue (ionic.bundle.js:22016)
    at ionic.bundle.js:22032
    at Scope.$eval (ionic.bundle.js:23228)

如果在 php 的 json 输出中读取 <,这意味着 php 代码部分存在错误。我很难调试 php 代码,因为 AngularJS 会自动解析来自 php 的数据。因此,我看不到错误的详细信息。此外,我真的怀疑这个简单的 php 脚本会不会出错。

提前致谢。

您的 PHP 代码可能触发了导致 HTML 响应的错误。

这很可能是因为您试图以数组索引的形式访问对象属性,从而触发类似

的事件

Fatal error: Cannot use object of type stdClass as array

将您的代码更改为

$post = json_decode($postData, true);

http://php.net/manual/function.json-decode.php#refsect1-function.json-decode-parameters


我还假设您正在使用 Content-Type: application/x-www-form-urlencoded 来保留请求 simple。我会推荐 text/plain,因为您的数据肯定不是 URL 编码的。

$http.post("http://" + host + "/mobile/register.php", {
  user_password_reg: $user_password,
  user_email_reg: $user_email,
  user_contact_reg: $user_contact,
  user_name_reg: $user_name,
  region: $reg_code,
  province: $prov_code,
  citymun: $citymun_code,
  barangay: $brgy_code,
  street: $street,
  echo: "1",
  success: "0",
  user_acc_type: "log account"
}, {
  headers: { 'Content-type': 'text/plain' }
})