我正在尝试使用此get
课程调用HttpClient
服务。它抛出了通常的CORS问题错误,但当我看到"响应"它返回我想要获得的答案信息(如图中所示):
我在课堂上做的是:
//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);
}
)
}
}
任何有助于找到答案的建议都将受到高度赞赏。