语法错误:使用 $http.post 和 restangular post 时出现意外的标记 F

SyntaxError: Unexpected token F, when using $http.post and restangular post

本文关键字:post 意外 restangular 错误 使用 http 语法      更新时间:2023-09-26

我们在当前的项目中使用了angular,我们正在使用Restangular进行API调用。突然,我在向某个端点发帖时开始出错。

帖子是这样的:

Restangular.one('aaa').post('bbb', data)
  .then((response) => {
    console.log(response);
  }, (err) => {
    console.log(err);
  });

日志是这样的:

SyntaxError: Unexpected token F

过了一会儿,我放弃了,并尝试使用角度$http服务。

$http({
  method: 'POST',
  url: '/aaa/bbb',
  data: data
}).then(function successCallback(response) {
  console.log(response);
  }, function errorCallback(response) {
    console.log(response);
  });

和日志:

SyntaxError: Unexpected token F

在那之后,我开始变得绝望,并用jQuery.ajax做了一篇文章。

jQuery.ajax({
  url:'/aaa/bbb',
  type: 'POST',
  data: data,
  contentType: 'application/json; charset=utf-8',
  success: function(s) {
    console.log(s);
    console.log('jquery success');
  },
  error: function(e) {
    console.log(e);
    console.log('jquery error');
  },
  complete: function(c) {
    console.log(c);
    console.log('jquery complete');
  }
});

以及使用 jquery.ajax 时的日志:

Object {readyState: 4, responseText: "Fatal error: actual error message here", status: 200, statusText: "OK"}
jquery error

终于有东西了!我们使用drupal作为我们的后端,其中一个模块没有启用。我只是想知道为什么restangular和$http未能给出正确的错误消息。

Restangular$http 模块生成的响应与从JSON模块收到的响应相同。例如:

JSON.parse("{F}");

产生错误:

未捕获的语法错误:意外的标记 F

来自jQuery的响应是来自服务器的原始响应数据,没有应用JSON.parse