json - 我需要从 Vue(SPA) 发送一个 json 对象到 Node (服务器)

标签 json node.js http vue.js

我正在尝试使用 Axion 将表单从 vue 应用程序发送到 Node 服务器。 但我无法在服务器端的请求中得到任何东西。 我在 vue 中有用户名和密码,但我无法将其发送到服务器进行处理,而且我可以通过 Postman 获得 x-www-form-urlencoded 类型的响应并查看服务器是否正常工作。 我尝试更改“Content-Type”的类型,但仍然没有在


服务器端。

服务器:

    router.options('/login',function(request, response, next) {
console.log(request.body);
    var login = methods.login(request.body.nationalID, request.body.password)
        .then((result) => {
            response.status(200).send({
                success: true,
                roles:   result.roles,
                token:   result.token
            })
        }).catch((err) => {
            response.status(err.code).send({
                success: false,
                message: err.message
            })
        })
});

router.post('/login',function(request, response, next) {

    console.log(request.body);
    var login = methods.login(request.body.nationalID, request.body.password)
        .then((result) => {
            response.status(200).send({
                success: true,
                roles:   result.roles,
                token:   result.token
            })
        }).catch((err) => {
            response.status(err.code).send({
                success: false,
                message: err.message
            })
        })
});

和服务器配置:

app.use(bodyParser.json()); // <--- Here
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

客户端:

const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({nationalID:username, password:password})
    };

    return axios.post('http://127.0.0.1:3000/user/login', requestOptions)
        .then(handleResponse)
        .then(user => {
            // login successful if there's a jwt token in the response
            if (user.token) {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('user', JSON.stringify(user));
            }

            return user;
        });

最佳答案

您过度使用了请求选项。

x-www-form-urlencoded 内容类型是 axios.post() 的默认值(以及几乎所有其他现有的 HTTP 库),并且有不需要 JSON.stringify() 任何东西。毕竟,您不想向服务器发送 JSON,而是发送表单编码数据。

直接发布一个对象即可:

return axios.post('http://127.0.0.1:3000/user/login', {
    nationalID: username,
    password: password
})
    .then( ... )

关于json - 我需要从 Vue(SPA) 发送一个 json 对象到 Node (服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675584/

相关文章:

angular - 如何用switchMap取消http请求?

python - 我如何实际使用 WSGI?

java - 无限滚动 ListView 不起作用

json - Angular2中如何从json中获取数据

node.js - Sourcemap "sources"数组链接到 "../../stdin"而不是实际的 SCSS 文件

c# - 如何使用 Thrift 的 C# THttpHandler?

javascript - 解析带引号的简单 json 时出现语法错误?

json - 如何在 logstash 中漂亮地打印电子邮件正文的 JSON?

javascript - AWS SDK 文件通过 Node/Express 使用流 PassThrough 上传到 S3 - 文件总是损坏

javascript - 在 Node.js/Express 中,如何自动将此 header 添加到每个 "render"响应中?