繁体   English   中英

SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置0) <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从发布中获取所有正确的参数,然后返回或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响应的错误。

这很可能是由于您试图将对象属性作为数组索引访问,从而触发了类似

致命错误:不能将stdClass类型的对象用作数组

将您的代码更改为

$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简化请求。 我建议您使用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' }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM