Bhushan Lodha Bhushan Lodha - 18 days ago 8
jQuery Question

Ajax PUT request not sending data

I have ajax put request which is not sending data to server

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);
}
});


What am I doing wrong?

Answer

I hope, you had a typo below instead of data, you had date. Please look at the comment below

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);
  }
});
Comments