首页 > 解决方案 > 431(请求标头字段太大)即使在清除缓存后,React 应用程序也会出错

问题描述

当我的 React 应用程序运行发布请求时,我收到“431(请求标头字段太大)”错误,我无法弄清楚是什么原因造成的。任何人都可以帮忙吗?

我根据类似问题的答案尝试过的事情:

表单提交 POST 请求的函数:

handleSubmit(evt){
    axios.post('/creator/new', {
        username: this.state.username,
        password: this.state.password,
        profilePic: this.state.profilePic,
        firstName: this.state.firstName,
        lastName: this.state.lastName,
        dob: this.state.dob,
        country: this.state.country
    }, {headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=UTF-8'
    }}).then(response => {
        console.log('response received');
    })
    evt.target.reset();
    this.props.history.push(`/creator/${this.state.username}`);
    this.props.addCreator(this.state);
}

服务器:

const express = require('express');
const cors = require('cors');

const creatorsRoutes = require('./routes/creators-routes');
const ipsumsRoutes = require('./routes/ipsums-routes');


const app = express();

app.use(cors());
app.use(express.json());

app.use((req, res, next) => {
  console.log('request received')
  res.send(req.path);
  console.log('request body sent back')
})

app.use('/creator', creatorsRoutes); 


const mongoose = require('mongoose');
mongoose.connect("mongodb+srv://SkyeWulff:************retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => {
      console.log('Connection open');
    })
    .catch(err => {
      console.log('Error occured:');
      console.log(err);
    })
let db = mongoose.connection;

db.on('error', console.error.bind(console, 'Connection Error'));

db.once('open', function(){console.log("successfully connected to MongoDB")});

app.listen(5000, () => {
  console.log("listening on port 5000");
})

服务器正在使用的路由器:

const express = require('express');
const creatorsController = require('../controllers/creators-controllers');

const router = express.Router();


router.get('/creator/id', creatorsController.getCreator);


router.post('/new', creatorsController.addCreator);

module.exports = router;

路由器使用的控制器:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const creatorSchema = new Schema({
    username: String,
    password: String,
    profilePic: String,
    firstName: String,
    lastName: String,
    dob: Date,
    country: String
});
module.exports = mongoose.model('Creator', creatorSchema);

文件树结构: 文件树

标签: reactjscookieshttp-headers

解决方案


https://stackoverflow.com/a/68092880/4437911中所述

(最大 http 标头大小参数可配置):
node --max-http-header-size 16000 client.js


推荐阅读