Angular 7:发送帖子请求给出错误 405(方法不允许)

Car*_*ara 0 post http http-status-code-405 angular

我在 Chrome 上有 CORS 扩展并且 GET 请求工作正常但 POST 请求没有。我收到错误“方法不允许”,但由于我有 CORS 扩展,根据文档,POST 请求应该可以工作。

我尝试使用另一个主题中提到的可选标头,但错误仍然存​​在。

我使用这个代码:

send(id){
    let list = JSON.stringify(this.array)
    this.http.post(this.url + this.id, list)
         .subscribe(),
            error => console.log("Error: ", error)
    )
} 
Run Code Online (Sandbox Code Playgroud)

浏览器控制台中的错误消息是:

消息:http://localhost:4000/ 的Http 失败响应...
名称:“HttpErrorResponse”
ok:false
status:405
statusText:“Method Not Allowed”

use*_*022 5

这大概与CORS.

405 方法不允许

通常用于指示服务器(对于目标资源)不接受HTTP 方法POST在您的情况下)。看看 405 的响应头 - 它应该包含这样的内容

Allow: GET, POST, HEAD
Run Code Online (Sandbox Code Playgroud)

有点可疑的是,您将令牌附加到请求 URL(假设它this.currentUservalueToken用于身份验证)。“通常”您会为此使用AuthorizationHTTP标头

尝试添加这样的Authorization标题:

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)
    )
}
Run Code Online (Sandbox Code Playgroud)