Angular http.get returns 404 在应用程序中,但来自 POSTMAN 正常
Angular http.get returns 404 in app but ok from POSTMAN
我正在尝试测试一个 API 调用。我能够使它从 POSTMAN 工作。当我尝试编写一个简单的 Angular 应用程序来获取结果时,我遇到了错误。
从错误的角度来看,它似乎要么将 localhost 附加到实际域中,要么将其保留但同时也保留了参数。为什么 URL 在第一个错误和第二个错误中的形成方式不同?为什么我从应用程序收到 404 错误,但 url 在 POSTMAN 中有效?
当 运行 为该应用程序提供服务时,我收到以下错误
后端返回代码 404,正文为:
Cannot
GET
/subdomain.domain.com/api/api/UserAccount/FindUser
我尝试以多种方式更改 URL 的格式,但其中 none 影响了最终结果。
app.component.ts:
export class AppComponent implements OnInit {
title = 'helloWorld bro';
private apicallsService : ApiCallsService;
constructor(acs: ApiCallsService)
{
this.apicallsService = acs;
}
ngOnInit() {
this.apicallsService.FindUser().subscribe(resp => { console.log(resp); });
}
}
apicalls.service.ts:
@Injectable()
export class ApiCallsService
{
constructor(private http : HttpClient ) { }
public API_URL: string = 'subdomain.domain.com/api';
public FindUser():Observable<ResponseDTO>
{
// let mail = (optionEmailForTesting != undefined) ? optionEmailForTesting : "";
let url = this.API_URL + '/api/UserAccount/FindUser';
const params = new HttpParams()
.set('firstName', 'John')
.set('lastName', 'Doe')
.set('dob', '1/1/1981')
.set('locale', 'en')
.set('optionEmailForTesting', '')
return this.http.get<ResponseDTO>(url,{params}).pipe(
catchError(this.handleError)
)
}
}
您需要将协议添加到 url 的开头,即
public API_URL: string = 'http://subdomain.domain.com/api';
如果没有这个,HttpClient 会将 url 视为相对的。
我正在尝试测试一个 API 调用。我能够使它从 POSTMAN 工作。当我尝试编写一个简单的 Angular 应用程序来获取结果时,我遇到了错误。
从错误的角度来看,它似乎要么将 localhost 附加到实际域中,要么将其保留但同时也保留了参数。为什么 URL 在第一个错误和第二个错误中的形成方式不同?为什么我从应用程序收到 404 错误,但 url 在 POSTMAN 中有效?
当 运行 为该应用程序提供服务时,我收到以下错误
后端返回代码 404,正文为:
Cannot GET /subdomain.domain.com/api/api/UserAccount/FindUser
我尝试以多种方式更改 URL 的格式,但其中 none 影响了最终结果。
app.component.ts:
export class AppComponent implements OnInit {
title = 'helloWorld bro';
private apicallsService : ApiCallsService;
constructor(acs: ApiCallsService)
{
this.apicallsService = acs;
}
ngOnInit() {
this.apicallsService.FindUser().subscribe(resp => { console.log(resp); });
}
}
apicalls.service.ts:
@Injectable()
export class ApiCallsService
{
constructor(private http : HttpClient ) { }
public API_URL: string = 'subdomain.domain.com/api';
public FindUser():Observable<ResponseDTO>
{
// let mail = (optionEmailForTesting != undefined) ? optionEmailForTesting : "";
let url = this.API_URL + '/api/UserAccount/FindUser';
const params = new HttpParams()
.set('firstName', 'John')
.set('lastName', 'Doe')
.set('dob', '1/1/1981')
.set('locale', 'en')
.set('optionEmailForTesting', '')
return this.http.get<ResponseDTO>(url,{params}).pipe(
catchError(this.handleError)
)
}
}
您需要将协议添加到 url 的开头,即
public API_URL: string = 'http://subdomain.domain.com/api';
如果没有这个,HttpClient 会将 url 视为相对的。