'Angular 7: Sending post request gives error 405 (Method not allowed)
I have the CORS extension on Chrome and the GET request works fine but the POST request does not. I get the error "method not allowed" but since I have the CORS extension, according to the documentation, a POST request should work.
I tried to use optional headers as mentioned in another topic but the error remains.
I use this code:
send(id){
let list = JSON.stringify(this.array)
this.http.post(this.url + this.id, list)
.subscribe(),
error => console.log("Error: ", error)
)
}
The error message in the browser console is:
message:Http failure response for http://localhost:4000/...
name: "HttpErrorResponse"
ok: false
status: 405
statusText: "Method Not Allowed"
Solution 1:[1]
this has probably nothing to do with CORS
.
405 Method Not Allowed
is generally used to indicate that the HTTP Method (POST
in your case) is not accepted by the server (for the target resource). Have a look at the response heades of the 405 - it should contain something like this
Allow: GET, POST, HEAD
ab bit suspicious is the fact that you append your token to the request URL (assuming that this.currentUservalueToken
is used for authentication). "Normally" you would use the Authorization
HTTP header for this.
try adding an Authorization
header like this:
send(ordernummer){
let arr = JSON.stringify(this.localeArray)
var headers = new Headers();
headers.append('Authorization', 'Bearer AADDFFKKKLLLL');
this.http.post(this.url+ ordernummer + "/" + this.id, arr, {
headers: headers
}).subscribe(data => console.log("Successful"),
error => console.log("Error: ", error)
)
}
Solution 2:[2]
.net core sample for sending number array :
if you use [Route("deleteAllWithIds/{ids}")] you get this error : "405 Method Not Allowed" in browser console.
[HttpPost]
[Route("deleteAllWithIds")]
public async Task<IActionResult> deleteAllWithIds([FromBody] List<int> ids)
{
}
public deleteAllWithIds(ids: number[]): Observable<any[]> {
return this.httpClient.post<any[]>(this.baseUrl + '/deleteAllWithIds', ids);
}
Solution 3:[3]
For future reference for anyone - worth checking your that http request matches the Http request in the Controller.
service.ts
this.http.post(path)
controller.cs
[HttpPost("path")]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | M Komaei |
Solution 3 | Robgit28 |