【发布时间】:2019-10-31 14:54:46
【问题描述】:
我正在尝试在我的 Vue 应用程序中提交表单数据。我有一个快速的后端 API。我试图发布到的端点在邮递员上完美运行。我不断收到“SyntaxError: Unexpected token g in JSON at position 0”或“400: Bad Request”
我尝试使用 JSON.parse(this.description)。我没有解析 this.description 就试过了。
在我的 axios 配置文件中,我尝试在我的 axios 响应拦截器中将响应标头更改为“application/json”。我也试过没有这样做。
这是表格
<v-dialog v-model="dialog" persistent max-width="600px">
<template v-slot:activator="{ on }">
<v-tooltip top>
<v-btn small fab color="white" dark v-on="on" slot="activator">
<v-icon color="primary">add</v-icon>
</v-btn>
<span>Add Task</span>
</v-tooltip>
</template>
<v-card>
<v-card-title>
<span class="headline">Add Task</span>
</v-card-title>
<v-card-text>
<v-form>
<v-textarea v-model="description" label="Description"></v-textarea>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" flat @click="dialog = false">Close</v-btn>
<v-btn color="blue darken-1" flat @click="addTask">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
这里是 axios 请求
methods: {
...mapActions(["fetchTasks"]),
addTask() {
console.log(this.description);
axios
.post("tasks", JSON.parse(this.description))
.then(response => {
dialog = "false";
})
.catch(err => console.log(err));
}
}
这是我的 axios 配置文件
"use strict";
import Vue from 'vue';
import axios from "axios";
import store from '../store';
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = '';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
baseURL: "http://localhost:3000/",
timeout: 60 * 1000, // Timeout
withCredentials: false, // Check cross-site Access-Control
};
const _axios = axios.create(config);
_axios.interceptors.request.use(
function (config) {
// Do something before request is sent
let token = store.getters.getToken;
if (token) {
config.headers.common.Authorization = token;
}
return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
);
// Add a response interceptor
_axios.interceptors.response.use(
function (response) {
// Do something with response data
return response;
},
function (error) {
// Do something with response error
return Promise.reject(error);
}
);
Plugin.install = function (Vue, options) {
Vue.axios = _axios;
window.axios = _axios;
Object.defineProperties(Vue.prototype, {
axios: {
get() {
return _axios;
}
},
$axios: {
get() {
return _axios;
}
},
});
};
Vue.use(Plugin)
export default Plugin;
这里是终点
router.post('/tasks', auth, async (req, res) => {
const task = new Task({
...req.body,
owner: req.user._id
});
try {
await task.save();
res.status(201).send(task);
} catch (err) {
res.status(400).send();
}
});
这是谷歌浏览器网络标签下的标题数据
【问题讨论】:
-
您的后端在请求正文中期望什么数据类型? JSON 或
application/x-www-form-urlencoded? -
期待 JSON。当我将内容类型更改为“application/json”时,我得到 400 Bad Request
-
那你为什么要设置Axios发送
application/x-www-form-urlencoded?我也不明白你对JSON.parse(this.description)的使用。您是否希望您的用户在 Description 字段中输入有效的 JSON?您想在请求 JSON 中发送哪些字段? 究竟您的后端期望什么? -
啊。愚蠢的错误。后端期待一个对象。我没有将对象传递给发布请求。
标签: javascript node.js express vue.js axios