[axios]数据未正确到达 req.body(axios post 方法)

2024-05-15 664 views
2

在 ReactJs 应用程序中执行 post 请求会生成以下 req.body: { '{"userEmail":"user@host.com","password":"12345"}': '' }

这是我的代码:

返回 axios({ url: ' http://localhost:3000/apitest ', 方法: 'post', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 数据: { userEmail : 'user@host.com', 密码: '12345' }, })

感谢您的任何帮助!

回答

7

您正在设置Content-Typeas application/x-www-form-urlencoded,这是不必要的。这是默认值Content-Type。如果您的数据是 JSON,就像您的情况一样,axios 会自动将 设为Content-Typeapplication/json;charset=utf-8但前提是Content-Type尚未手动设置 。尝试删除您的headers并查看这是否可以解决您的问题。

3

感谢您的快速反馈。我尝试删除标头,并尝试将 Content-Type 设置为“application/json;charset=utf-8” -> 在这两种情况下,请求都不会“到达”节点/express 应用程序。并且在浏览器中显示以下错误消息:

“选项http://localhost:3000/apitest xhrAdapter @bundle.js:25307(匿名函数)@bundle.js:25155dispatchRequest@bundle.js:25151”

“XMLHttpRequest 无法加载http://localhost:3000/apicheckuser。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin“null”为不允许访问。响应的 HTTP 状态代码为 404。”

6

听起来您在 CORS 方面遇到了问题。您是否向与托管网页的来源不同的来源发出请求?您可能需要通过使用OPTIONS请求方法响应请求并设置标头来在服务器上处理此问题Access-Control-Allow-Origin。您可以将其设置为特定域Access-Control-Allow-Origin: http://example.com或允许通配符 Access-Control-Allow-Origin: *

7

谢谢 - 我也在研究我的 CORS 设置,但我已经允许通配符访问:

res.header('Access-Control-Allow-Origin', '*');

我将研究我似乎缺少的 OPTIONS 请求方法。我会检查并回来。谢谢

5

感谢您的反馈,我正确地实现了 OPTIONS 请求方法(在堆栈溢出中找到它):

var allowedCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('访问控制允许方法', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', '内容类型、授权、内容长度、X-Requested-With'); // 拦截 OPTIONS 方法 if ('OPTIONS' == req.method) { res.sendStatus(200); } else { 下一个(); } };

app.use(allowCrossDomain);

1

很高兴你能成功:)

6

没想到我对这个问题的引用会出现在这里,抱歉。我已经删除了我的存储库中的链接,但似乎 ref 仍然存在 如果其他人找到此线程,请升级到 Express 4 以查看 req.body 中的帖子有效负载数据。

4

当我执行 POST 时,我仍然看到这个问题,req.body 仍然是空的。我当前运行的是 Node v.6.6.0

4

您好,我在 POST 方面也遇到类似的困难,req.body 保持未定义。我使用的是 0.15.3 版本的 axios 在浏览器端“index.vue”,相关代码是:

axios.post('donnees/member/create', { 伪: this.pseudo, 电子邮件: this.email, 密码: this.password, Inscription_Date: '' }) .then(function (response) { console.log(response ) }) .catch(函数(错误) { console.log(错误) })

在服务器端,路由已确定,但 req.body 保持未定义

Exports.member_create_post = function(req, res, next) { console.log('BODY ' + req.body) }

我不知道如何进一步处理我的错误,有什么想法吗?

非常感谢

马蒂厄

4

有用 !我没有使用模块“body-parse”: app.use(bodyParser.json()) // 处理 json 数据 app.use(bodyParser.urlencoded({ Extended: true })) // 处理 URL 编码数据 Matthieu

1

当我通过 laravel resources 路由 vue axios 提交表单时,会生成此错误 截图_1 截图_2 POST http://localhost/task 404 (Not Found)

2

@MatthieuHPPbody-parser不是body-parse

3

感谢您的反馈,我正确地实现了 OPTIONS 请求方法(在堆栈溢出中找到它):

var allowedCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('访问控制允许方法', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', '内容类型、授权、内容长度、X-Requested-With'); // 拦截 OPTIONS 方法 if ('OPTIONS' == req.method) { res.sendStatus(200); } else { 下一个(); } };

app.use(allowCrossDomain);

嗨,谢谢你,它对我有用。

1

npm i——保存cors

const express = require('express')
const app = express()
const bodyParses = require('body-parser');
const cors = require('cors')

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
  cors({
    origin: "http://localhost:1234",
    credentials: true,
  })
);
app.use('/', req, res, next ....
app.listent..