且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 2 基本身份验证不起作用

更新时间:2023-12-03 22:40:16

为您的请求添加自定义标头的简化版本:

Simplified version to add custom headers to your request:

import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';

@Injectable()
export class ApiService {

   constructor(private _http: Http) {}

   call(url): Observable<any> {
      let username: string = 'username';
      let password: string = 'password';
      let headers: Headers = new Headers();
      headers.append("Authorization", "Basic " + btoa(username + ":" + password)); 
      headers.append("Content-Type", "application/x-www-form-urlencoded");
      return this._http.post(url, data, {headers: headers})
    }
}

很难确定你哪里出错了,因为缺少实际 http 调用的代码.但是这个例子应该适合你

It's hard to determine where you went wrong, because the lack of code of the actual http call you do. But this example should work for you