How to Fix “HTTP 405 method not allowed” Error

In the Angular 1.4 version of the project, the program was working fine until one day when the form submission prompted an “HTTP 405” error — “Method Not Allowed”.
Either GET or POST items GET interface submit data, but check back and back again and again. Both are POST. No problem at all.
After careful examination of the front-end code, it is found that the writing method is as follows:

$http({
    method : 'POST',
    url : '/test',
    params : {
        cycle : key,
        emp_id : user.id
    }
 }).success(function (resp) {
 });

This programming approach has two problems:
1. The submitted parameters are exposed; 2. The default Header parameter “content-type” submitted is “application/json”;
But after trial and error, see Prioritizing Browser Query Parameters and Form Data. The first problem doesn’t cause 405 errors, so it’s easy to identify the problem. The solution is to specify the “content-Type” explicitly, as follows:

$http({
    method : 'POST',
    url : '/test',
    params : {
        cycle : key,
        emp_id : user.id
    },
    //  New content-type header attribute
    heads : {
        'content-type' : 'application/x-www-form-urlencoded'
    }
 }).success(function (resp) {
    //  Processing Logic
 });

If you want to solve the first problem, you only need to introduce the $httpParamSerializer service as follows:

$http({
    method : 'POST',
    url : '/test',
    //  Submit as a form, converting Object to form arguments.
    data : $httpParamSerializer({
        cycle : key,
        emp_id : user.id
    }),
    //  New content-type header attribute
    heads : {
        'content-type' : 'application/x-www-form-urlencoded'
    }
 }).success(function (resp) {
    //  Processing Logic
 });

conclusion
In the event of an HTTP 405 error, first check the “content-Type” information in the request header.

Read More: