"SyntaxError: Unexpected token <" with Ionic Angular JS

"SyntaxError: Unexpected token <" with Ionic Angular JS

我正在使用 Angular JS 在我的 CakePHP 服务器上调用 API。它将新客户添加到 MySQL 数据库:

  $scope.submitForm = function(isValid) {
    if (isValid) {
      $http({
        method  : 'POST',
        url     : 'https://www.something.com/customers/add.json',
        data    : $.param($scope.formData), 
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  
      })
      .success(function(data) {
        console.log(data);
        if (data.success == false) {
          $scope.error = data.message;
        } else {
          $scope.message = data.message; 
        }
      });
    }
  }

当添加新实体失败时,我得到正确答案(如果条件):

当可以添加实体时我得到这个(其他条件):

SyntaxError: Unexpected token <
    at Object.parse (native)
    at fromJson (http://localhost:8100/lib/ionic/js/ionic.bundle.js:10063:14)
    at defaultHttpResponseTransform (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18080:16)
    at http://localhost:8100/lib/ionic/js/ionic.bundle.js:18171:12
    at forEach (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9168:20)
    at transformData (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18170:3)
    at transformResponse (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18926:23)
    at processQueue (http://localhost:8100/lib/ionic/js/ionic.bundle.js:23399:28)
    at http://localhost:8100/lib/ionic/js/ionic.bundle.js:23415:27
    at Scope.$eval (http://localhost:8100/lib/ionic/js/ionic.bundle.js:24678:28)

在服务器端(生成答案的地方)看起来像这样:

if ($this->Customer->save($this->request->data)) {
    $message = 'Adding customer was successful, thanks!';
    $success = true;
} else {
    $message = 'Adding customer failed, please try again.';
    $success = false;
}

$this->set('_serialize', array('message', 'success'));
$this->set(compact('message', 'success'));

为什么我总是收到 else 条件的语法错误?

如评论中所述...这是因为成功响应无效 JSON。

尝试调试控制台中的输出并相应地修复 ;-)