gpt4 book ai didi

javascript - "SyntaxError: Unexpected token <"与 Ionic Angular JS

转载 作者:行者123 更新时间:2023-12-02 15:16:29 27 4
gpt4 key购买 nike

我正在使用 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;
}
});
}
}

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

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

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。

尝试在控制台中调试输出并进行相应修复;-)

关于javascript - "SyntaxError: Unexpected token <"与 Ionic Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com