简体   繁体   English

SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置0) <anonymous> )-AngularJS

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

I am quite certain that all the arguments I passed into register() have values. 我很确定我传递给register()所有参数都有值。 This is the js code. 这是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);
    });


}

This is the php script 这是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);
 ?>

This programs is still not complete. 该程序仍未完成。 I first wanted to make sure that the php is getting all the right parameters from post and returns or echo es a right value. 我首先要确保php从发布中获取所有正确的参数,然后返回或echo正确的值。 However, in these code I get this error in the console. 但是,在这些代码中,我在控制台中收到此错误。

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)

if < is read in the json output of the php, that means there was an error on the part of the php code. 如果在PHP的json输出中读取<则表示PHP代码部分存在错误。 It's quite hard for me to debug the php code since the AngularJS automatically parses the data from the php. 对于我来说,调试php代码非常困难,因为AngularJS会自动解析php中的数据。 Hence, I cannot see the details of the error. 因此,我看不到错误的详细信息。 And besides I really doubt that this simple php script would have an error. 而且我真的很怀疑这个简单的php脚本会有错误。

Thanks in advance. 提前致谢。

Your PHP code is probably triggering an error resulting in an HTML response. 您的PHP代码可能触发了导致HTML响应的错误。

This is most likely due to the fact you're attempting to access object properties as array indices, triggering something like 这很可能是由于您试图将对象属性作为数组索引访问,从而触发了类似

Fatal error: Cannot use object of type stdClass as array 致命错误:不能将stdClass类型的对象用作数组

Change your code to 将您的代码更改为

$post = json_decode($postData, true);

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


I also assume you're using Content-Type: application/x-www-form-urlencoded to keep the request simple . 我还假设您正在使用Content-Type: application/x-www-form-urlencoded简化请求。 I would recommend text/plain instead as your data is certainly not URL encoded. 我建议您使用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.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> ) 在 XMLHttpRequest。<anonymous></anonymous></anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.<anonymous> Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> 显示SyntaxError的json响应:意外令牌[JSON中JSON.parse( <anonymous> ) - json response showing SyntaxError: Unexpected token [ in JSON at position 23 at JSON.parse (<anonymous>) jQuery.Deferred exception: Unexpected token &lt; in JSON at position 0 SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> )</anonymous> - jQuery.Deferred exception: Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> ) - VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: JSON JSON JSON.parse 0 中的意外标记 - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse 未捕获的语法错误:JSON 中的意外令牌 < position 中的 0 JSON.parse - Wordpress - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse - Wordpress JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM