gpt4 book ai didi

angularjs - 正文解析器 : Unexpected Token -

转载 作者:搜寻专家 更新时间:2023-10-31 23:41:06 25 4
gpt4 key购买 nike

我实际上正在开发一个带有文件上传和一些数据的 Angular 网络表单。以下是请求 header :

POST /tests/add HTTP/1.1
Host: localhost:3000
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Referer: http://localhost:8000/
Content-Length: 2033

我这样构建请求:

var formData = new FormData();
formData.append('file', $scope.test.file);
$http({
method: 'POST',
url: backUrl + '/tests/add',
data: { 'file': $scope.file,
'token': 'test'},
contentType: false,
processData: false,
transformRequest: function (data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
var headers = headersGetter();
delete headers['Content-Type'];
return formData;
}

但它总是返回一个错误的 400 错误请求:

Unexpected token -
400

SyntaxError: Unexpected token -
at parse (/home/me/projects/www/node_modules/body-parser/lib/types/json.js:83:15)
at /home/me/projects/www/node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/home/me/projects/www/node_modules/raw-body/index.js:262:16)
at done (/home/me/projects/www/node_modules/raw-body/index.js:251:7)
at IncomingMessage.onEnd (/home/me/projects/www/node_modules/raw-body/index.js:307:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)

这是我后端中对 bodyParser 的唯一引用:

app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true,
defer: true
}));

我做错了什么?

最佳答案

您确定要发送 JSON 吗?

很可能您使用的是无效的 JSON,这会导致 bodyParser.json() 出错。

确保您使用的是有效的 JSON。您还可以将 bodyParser.json 与 bodyParser.raw 交换为中间件。

关于angularjs - 正文解析器 : Unexpected Token -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381045/

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