标题问题(Angular):服务器在同一请求中以401和200响应?

时间:2017-10-31 14:04:45

标签: angular http header authorization

我有这个小应用程序要求API的服务。我已经设置了这样的凭据:

this.http.get<Response>(url, {
        headers: new HttpHeaders().set('Authorization', 'Basic ' 
        + btoa('C7YHKF91RAABYXNK :JMGYBMAT1X18KA2W '))
      })
        .subscribe(data => {
          Response = data['results'];

但日志是&#34;奇怪&#34;: enter image description here

有一个401然后一个200而且API没有返回被返回的东西(一个png图像)。在Edge上,当我点击提交时,浏览器会要求提供令牌。我输入凭据,然后服务工作(铬是类似的)...昨天一切都工作正常,但现在我有这个问题。它是API吗?标头是否设置错误?我有一个用于测试here的实时演示。谢谢你的帮助!

按照@Mudassar

的建议进行编辑
onSubmit({ valid }: { valid: boolean }, f: any) {

    if {//
    } 
    else {

      let color = encodeURIComponent(this.params.handwriting_color);
      const url =
        `https://api.handwriting.io/render/png?handwriting_id=${this.params.handwriting_id}&text=${this.params.text}&handwriting_size=${this.params.handwriting_size}&handwriting_color=${color}`;

      //SET HEADERS
      let headers= new HttpHeaders();
      headers.append('Authorization', 'Basic ' + btoa('C7YHKF91RAABYXNK :JMGYBMAT1X18KA2W '))
headers.append("Access-Control-Allow-Origin", "new[] { (string)context.Request.Headers['Origin'] }");
      headers.append("Access-Control-Allow-Headers", "new[] { 'Origin, X-Requested-With, Content-Type, Accept, Authorization' }");
      headers.append("Access-Control-Allow-Methods", "new[] { 'GET, POST, PUT, DELETE, OPTIONS' }");
      headers.append("Access-Control-Allow-Credentials", "new[] { 'true' }");




      //REQUEST
      this.http.get<Response>(url, {
        headers: headers
      })
        .subscribe(data => {
          Response = data['results'];

        }, (err: HttpErrorResponse) => {
          if (err.error instanceof Error) {
            console.log('An error occurred:', err.error.message);
          } else {
            console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
            console.log(Response);
          }
        });
    }

但我仍然得到401 ......

1 个答案:

答案 0 :(得分:1)

这可能是由于没有设置CORS

Toolbar.setOnTopSideMenu(false)

进一步设置这应确保正常工作

select A.City,B.Color,B.type from A cross join B where A.City = 'tehran';

OR 

select A.City,B.Color,B.type from A,B where A.City = 'tehran';

编辑:开始觉得问题可能与服务器端有关,因此建议在服务器端添加以公开Authorization标头。  Access-Control-Allow-Headers: Authorization