Angular 7 : Sending post request gives error 405 (Method not allowed)

标签 angular http post http-status-code-405

我在 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/

相关文章:

css - Angular 4 ngStyle 背景图像使页面变慢。滚动时无限期调用的函数

java - httpURLConnection 与 apache commons http

http - gmail 如何让用户保持登录状态?

c# - 在 C# ASP.NET 中获取发布值

python - Django - 从表单中的列表中的列表获取POST数据

angular - 将多个值传递给 Angular 6 中的管道

css - 更改 ion-datetime 值的颜色

http - WebKit 是否缓存第三方资源?

javascript - Post请求在服务器端返回未定义

angular - 如何仅在 angular2 中的文本区域模糊上更新模型