node.js - 无法加载资源: the server responded with a status of 431 (Request Header Fields Too Large)

标签 node.js reactjs mongodb http http-headers

当我尝试使用 React 应用程序中的 NodeJS api(构建 MERN 堆栈应用程序)时,我收到了相关错误 “无法加载资源:服务器响应状态为 431(请求 header 字段太大)”

postman 的 API 工作正常

const onSubmit=async(e)=>{
    e.preventDefault()
    if(password!==password2){
        console.log('passwords dont match')
    }else{
        const newUser={
            name:name,
            email:email,
            password:password
        }
        try {
            const config={
                headers:{
                    'Content-Type':'application/json'
                }
            }
            const body=JSON.stringify(newUser)

            //axios has been set up as proxy
            //http://localhost:3000
            //we dont need to add the above to url
            const res =await axios.post('/api/users',body,config)
            console.log(res.data)

        } catch (error) {
            console.error(error.response.data)
        }
    }
}

最佳答案

The HTTP 431 Request Header Fields Too Large response status code indicates that the server refuses to process the request because the request’s HTTP headers are too long. The request may be resubmitted after reducing the size of the request headers. 431 can be used when the total size of request headers is too large, or when a single header field is too large. To help those running into this error, indicate which of the > two is the problem in the response body — ideally, also include which headers are too large. This lets users attempt to fix the problem, such as by clearing their cookies. Servers will often produce this status if: The Referer URL is too long There are too many Cookies sent in the request

就我而言,我发送了太多 cookie,因为 localhost:4200 被用作 3 个不同项目的域......解决方案(删除无用的 cookie)

希望这有帮助...

关于node.js - 无法加载资源: the server responded with a status of 431 (Request Header Fields Too Large),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976846/

相关文章:

mongodb - Play Framework 2.1 的最佳 mongodb 驱动程序

node.js - 如果 npm 测试失败,npm posttest 不会触发

multithreading - 使用rabbitmq与多线程之间的区别

node.js - Hapijs - 服务器重新启动时 HttpOnly cookie 消失

reactjs - 在 meteor / react 框架中将当前用户设置为 session 变量是一个坏主意吗?

reactjs - 在我的表单中使用 react 评级组件时如何设置值的状态?

javascript - 当该元素的值更改时,是否会自动重新执行从元素读取值的 Meteor Helper?

javascript - 如何并行执行更新

javascript - Redux 工具包 createAsyncThunk 问题 : state updates after async dispatch call?

mongodb - 如何填写 Docker mongodb 镜像