简体   繁体   English

Angular 2 -POST 401 未经授权

[英]Angular 2 -POST 401 Unauthorized

I am using angular 2. I want to post data using API, but I get unauthorized access error...我正在使用 angular 2。我想使用 API 发布数据,但出现未经授权的访问错误...

Can someone help me figure out why am I getting this error?有人能帮我弄清楚为什么我会收到这个错误吗?

401 未授权

Check with the headers you configured for Authorization in service.ts file.检查您在 service.ts 文件中为授权配置的标头。 If it's not you need to add the headers to the URL.如果不是,则需要将标头添加到 URL。

Headers can be used as标题可以用作

const httpOptions = {
    headers: new HttpHeaders({
        'Authorization': 'Bearer ' + localStorage.getItem('token')
    })
} 

And add this to the URL as follows.并将其添加到 URL 中,如下所示。 As and example:例如:

getUsers() {
    return this.http.get(this.baseUrl + 'users', httpOptions);
}

Check your API authentication scheme.检查您的 API 身份验证方案。 you might need to login first ( if using cookies ).您可能需要先登录(如果使用 cookie)。 Or provide some authentication header.或者提供一些身份验证标头。

Your problem is missing or wrong content type header.您的问题是缺少或错误的内容类型标题。

For post method requests your content type should be one of the standard form types: application/x-www-form-urlencoded or multipart/form-data对于 post 方法请求,您的内容类型应该是标准表单类型之一:application/x-www-form-urlencoded 或 multipart/form-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM