我在 Chrome 上有 CORS 扩展,GET 请求工作正常,但 POST 请求没有。我收到错误“不允许的方法”,但由于我有 CORS 扩展,根据文档,POST 请求应该有效。
我尝试使用另一个主题中提到的可选 header ,但错误仍然存在。
我使用这段代码:
send(id){
let list = JSON.stringify(this.array)
this.http.post(this.url + this.id, list)
.subscribe(),
error => console.log("Error: ", error)
)
}
浏览器控制台的错误信息是:
message:Http failure response for http://localhost:4000/...
name: "HttpErrorResponse"
ok: false
status: 405
statusText: "Method Not Allowed"
最佳答案
这可能与 CORS
无关。
405 Method Not Allowed
通常用于指示服务器(对于目标资源)不接受HTTP 方法(在您的情况下为POST
)。看看 405 的响应头 - 它应该包含这样的内容
Allow: GET, POST, HEAD
有点可疑的是您将 token 附加到请求 URL(假设 this.currentUservalueToken
用于身份验证)。 “通常”您会为此使用 Authorization
HTTP header。
尝试像这样添加一个 Authorization
header :
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)
)
}
关于 Angular 7 : Sending post request gives error 405 (Method not allowed),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57918922/