我有一个不向服务器发送数据的ajax put请求
handleUpdate(meal) {
console.log(meal)
var meals = this.state.meals;
$.ajax({
url: `api/v2/meals/${meal.id}`,
type: 'PUT',
date: {name: meal.name, calories: meal.calories, meal_time: meal.meal_time, meal_date: meal.meal_date},
headers: window.Auth.retrieveData('authHeaders'),
success: (new_meal) => {
this.setState({
meals: meals.map(meal => meal.id === new_meal.id ? new_meal : meal)
})
},
error: (response) => {
console.log(response);
}
});
我做错了什么?
答案 0 :(得分:3)
我希望你有一个typo
而不是data
,你有date
。请看下面的评论
handleUpdate(meal) {
console.log(meal)
var meals = this.state.meals;
$.ajax({
url: 'api/v2/meals/${meal.id}',
type: 'PUT',
// The below should be 'data'
date: {name: meal.name, calories: meal.calories, meal_time: meal.meal_time, meal_date: meal.meal_date},
headers: window.Auth.retrieveData('authHeaders'),
success: (new_meal) => {
this.setState({
meals: meals.map(meal => meal.id === new_meal.id ? new_meal : meal)
})
},
error: (response) => {
console.log(response);
}
});
答案 1 :(得分:1)
我注意到url
你在使用不同的单引号,请确保使用正确的单引号,这有时会发生。
正确的应该是'api/v2/meals/${meal.id}'
此外,请确保您遵循调用的良好示例,以确保成功传递其他属性。