[Vuejs]-Axios Post return 405 Method Not Allowed on Vue.js

0👍

The problem is that axios made a CORS request before the POST, and the NET Core API should be configured to accept CORS request.

I’ve found an article (here) that saying the problem for this cases is that IIS does not accept CORS request and the CORS module should be installed .
I’ve tried this change but the result was the I was receving an HTTP/1.1 204 No Content instead of a 403.

In my case the problem was the API service itself.

The CORS should be enabled in the API Service.
In NET CORE 5 for a basic configuration it’s enough to add the CORS services in Startup

services.AddCors();

and configure it

    app.UseCors(builder =>
    {
        builder
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
    });

With these changes the service works perfectly for any vue.js requests coming from axios.

0👍

To send the data as JSON in the body of the post request, just add a parameter "data" to the axios options. Axios will automatically set the content-tpe to the proper value for you.

// Simple example
axios.post(myUrl, {
  data: {
    "username":"user",
    "password":"testpw"
  }
});

This should have the expected outcome 🙂

Leave a comment