gpt4 book ai didi

node.js - ExpressJS 请求对象

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:45 25 4
gpt4 key购买 nike

我正在尝试查看 expressJS tutorial 。这是我的服务器代码:

var express = require('express');
var app = require('express').createServer();

app.use(express.bodyParser());

app.post('/', function(request, response) {
console.log('Inside the post request!');
console.log(request);
response.send(resquest.body);
});

app.listen(3000);

这是我正在模拟的 POST 请求:

$.ajax({
url: 'http://localhost:3000',
type: 'POST',
datatype: 'json',
data: {hello: 1},
success: function () {
console.log('Success!');
},
error: function () {
console.log('Error!');
}
});

问题在于request对象似乎不包含data: {hello: 1}。相反,它是一大堆底层参数。我是不是做了什么蠢事?

最佳答案

我认为既然你没有设置 content type对于 multipart/form-data ,它假设表单编码数据。在这种情况下,您可以将 ajax 请求中的数据设置为:

data: 'hello=1'

将您的内容类型设置为:application/x-www-form-urlencoded

通过 request.body.hello 访问它。已经有一段时间了,但是尝试一下。

关于node.js - ExpressJS 请求对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362231/

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