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:
- Vue: How to Fix “not displaying the holder in IE9 and below”
- How to Fix Taro SwipeAction Not Working Issue
- [Solved] Error:Plugin/Preset files are not allowed to export objects, only functions
- [Weex]Error in creating project NPM: unable to load file D:\program files\nodejs\node_ global\ weex.ps1 Because scripts are not allowed to run on this system.
- How to Solve Files Upload Error: http://net::ERR_SSL_PROTOCOL_ERROR
- [Solved] Cnpm installation -g@ view/cli Error: error: eperm: operation not allowed…
- The solution of calling$. Ajax successfully but the success method does not respond
- @requestbody: How to Use or Not Use
- [Solved] Error: #error More than 1 blank line not allowed no-multiple-empty-lines
- How to download files by post submission under Ajax
- How to Test whether the property value of each property in the object is not empty
- [Solved] JQuery each Method Error: $XXX is not defined
- Vue displays 404 and 500 interfaces according to HTTP response status
- Method to solve uncaught typeerror: cannot set property ‘onclick’ of null error
- Vue2.0: How to Use vue3 api to encapsulate Axios
- How to Solve “Vue is not defined” Error
- [Solved] node.js Upload Files Error: Multipart: boundary not found multer
- How to Solve OffsetParent is not set error
- ERROR in [eslint] ESLint is not a constructor (How to Solve)
- Asynchronous loading method of Baidu map