Aatish Kumar
Aatish Kumar

Reputation: 149

PUT works with POSTMAN but not with AXIOS

I am trying to update a record in the database. The back end is in C# ASP.NET and the front end is in vuejs. GET, POST methods work with POSTMAN and AXIOS. PUT method do works with POSTMAN but not with axios. Below is the trimmed version of the axios.

axios({
  method: 'PUT',
  url: 'http://localhost:64427/api/employees/update',
  header:{
      'Content-Type': 'application/json'
  },
  data: {
       QUAD: n_emp.QUAD,
       EMAIL: n_emp.EMAIL,
       ACTIF_O_N:n_emp.ACTIF_O_N
  }
}).then(response => {
   this.$router.push('/management/employee/overview')
}

Below is the controller

 [HttpPut]
 [ActionName("update")]
 public HttpResponseMessage UpdateEmployee([FromBody]EmployeeFormVM employee)
 {
    var db = new KronosDB("public");
    var response = Request.CreateResponse();
    EMPLOYEE updateemployee = db.Employees.Where(e => e.QUAD == employee.QUAD).FirstOrDefault();
    updateemployee.QUAD = employee.QUAD;
    updateemployee.ACTIF_O_N = employee.ACTIF_O_N;
    updateemployee.EMAIL = employee.EMAIL;
 }

I have this at the top of the controller.

[EnableCors(origins: "http://localhost:8080", headers: "*", methods: "*")]

I get the message below

Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.

Upvotes: 1

Views: 931

Answers (1)

Aatish Kumar
Aatish Kumar

Reputation: 149

Following the instructions from documentation

Enabling Cross-Origin Requests in ASP.NET Web API 2

I added these lines below in WebApiConfig.cs in the Register function to solve this issue.

config.EnableCors();

Upvotes: 1

Related Questions