export const add = (data) => { return axios.post(`${API_URL}/board/add.html`, (data)) .then(response => response.data) .catch(error => { throw error; }); }; 当前接口后端拿不到传递的数据
时间: 2024-02-27 19:57:36 浏览: 17
您好,根据您提供的代码,似乎是将数据作为一个对象传递给了 `axios.post` 方法,但是在后端却无法获取到数据。可能是因为在发送请求时,数据需要以 `application/json` 的格式传递,而您的代码中未指定请求头。您可以尝试在 `axios.post` 方法中加入如下代码来指定请求头:
```
axios.post(`${API_URL}/board/add.html`, data, { headers: { 'Content-Type': 'application/json' } })
```
这样就可以确保数据以正确的格式传递给后端了。同时,您也可以在后端中添加相应的处理来接收传递过来的数据。
相关问题
修改代码 const add=()=>{ axios.post("/shelf/add",{ user_id:localStorage.getItem("id"), book_id:route.query.bookId }).then((res)=>{ const data = res.data console.log(data) }) }
这段代码是一个使用 axios 发送 POST 请求的函数,请求地址是 `/shelf/add`,请求参数是当前用户的 id 和书本的 id。当请求成功后,会将响应数据打印到控制台。如果要修改这个函数,可以根据实际需求修改请求地址和请求参数,如下所示:
```javascript
const add = () => {
axios.post("/api/add-to-cart", {
userId: localStorage.getItem("userId"),
productId: route.query.productId,
quantity: 1
}).then((res) => {
const data = res.data;
console.log(data);
});
};
```
这个例子中,我们将请求地址改为了 `/api/add-to-cart`,请求参数包括了用户的 id、产品的 id 和购买数量,用于向购物车添加商品。你可以根据具体的业务需求修改这些参数。
这是api: export const latest = data => { return axios.post("/wms/inter/order/latestInterOrder", data); }; 这是前端代码: <el-dialog :visible.sync="showInterOrderDialog"></el-dialog> 这是前端script: import { latest } from "@/api/wms/inter-order"; data() { return { }; }, mounted() { this.showInterOrderDialog = false; latest().then(response => { const interOrder = response.data; this.lastInterOrderId = interOrder.id; }).catch(error => { console.error("报错信息:", error); }); this.pollLatestInterOrder(); }, methods: { newMessage() { this.$message({ showClose: true, message: "有新货物入库!" }); }, async pollLatestInterOrder() { }, }, 登录之后,怎么将前端的userId传递给后端接口
可以在调用 latest 接口的时候,将 userId 作为参数传递给后端接口。修改 latest 函数如下:
```
export const latest = (userId, data) => {
return axios.post(`/wms/inter/order/latestInterOrder?userId=${userId}`, data);
};
```
然后在 mounted 方法中调用 latest 函数时,将 userId 传递给它:
```
mounted() {
this.showInterOrderDialog = false;
const userId = this.$store.state.user.id;
latest(userId).then(response => {
const interOrder = response.data;
this.lastInterOrderId = interOrder.id;
}).catch(error => {
console.error("报错信息:", error);
});
this.pollLatestInterOrder();
},
```
这样后端接口就可以拿到前端传递过来的 userId 参数了。