Angular,get throws Access-Control-Allow-Origin但仍然返回答案,我怎么能得到它?

时间:2018-04-21 19:52:24

标签: angular get cors

我正在尝试使用此get课程调用HttpClient服务。它抛出了通常的CORS问题错误,但当我看到"响应"它返回我想要获得的答案信息(如图中所示):

error but correct answer

我在课堂上做的是:

//HttpclientmanagerService class
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from "rxjs/Observable";

@Injectable()
export class HttpclientmanagerService {

  constructor(
            private http: HttpClient
          ) { }

  login(username): Observable<any>{
      let url = 'http://localhost:9666/loginUserServlet?user='+username.toString();
      return this.http.get(url);
  }

}

和调用者类:

import { Component, OnInit } from '@angular/core';
import {HttpclientmanagerService} from "../services/httpUtils/httpclientmanager.service";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(private httpmngr: HttpclientmanagerService) { }

  ngOnInit() {}

  login(){
    this.httpmngr.login('usertest').subscribe(
                      result => {

                          if(result.code != 200){
                            console.log('result data (200):\n'+result);
                          }else{
                            console.log('result data:\n'+result.data);
                          }
                        },
                      error => {
                          console.log(<any>error);
                        }
                    )
  }

}

任何有助于找到答案的建议都将受到高度赞赏。

0 个答案:

没有答案