如何使用 React js 中的 put 请求将当前日期时间发送到 Rest Api?
How to send current date time to Rest Api with put request in react js?
我在 react js 中使用 axios 在 put 请求中发送数据。它运行良好,但是当我尝试使用 put 请求发送当前日期时间时,它给我错误。
Bad Request: /buyer/OrdersAPI/14/
[03/Oct/2021 14:19:44] "PUT /buyer/OrdersAPI/14/ HTTP/1.1" 400 132
我必须post当前日期和时间才能休息api日期时间字段
我的代码
const api = axios.create({
baseURL: 'http://localhost:8000/buyer/'
})
updateOrderStatus = async(id,status) => {
let data = new FormData();
data.append('status',status );
data.append('orderStartTime',Date())
await api.put(`OrdersAPI/${id}/`, data);
this.getNewOrders();
}
如果我 console.log(Date()) 它给我 Sun Oct 03 2021 14:28:59 GMT+0500 (Pakistan Standard Time)
问题是您传递了一个 Date
对象,因此要修复您需要将其转换为 json
格式的问题
new Date().toJSON()
这将return这样的结果
console.log(new Date().toJSON())
我在 react js 中使用 axios 在 put 请求中发送数据。它运行良好,但是当我尝试使用 put 请求发送当前日期时间时,它给我错误。
Bad Request: /buyer/OrdersAPI/14/
[03/Oct/2021 14:19:44] "PUT /buyer/OrdersAPI/14/ HTTP/1.1" 400 132
我必须post当前日期和时间才能休息api日期时间字段
我的代码
const api = axios.create({
baseURL: 'http://localhost:8000/buyer/'
})
updateOrderStatus = async(id,status) => {
let data = new FormData();
data.append('status',status );
data.append('orderStartTime',Date())
await api.put(`OrdersAPI/${id}/`, data);
this.getNewOrders();
}
如果我 console.log(Date()) 它给我 Sun Oct 03 2021 14:28:59 GMT+0500 (Pakistan Standard Time)
问题是您传递了一个 Date
对象,因此要修复您需要将其转换为 json
格式的问题
new Date().toJSON()
这将return这样的结果
console.log(new Date().toJSON())