Angular http get 请求什么都不做
Angular http get request do nothing
所以,我正在尝试向 API 发出请求(例如 SWAPI API)
在我的 app.module 中:
...
...
import { HttpClientModule } from '@angular/common/http';
declarations: [
AppComponent,
...
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientTestingModule,
HttpClientModule,
...
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在我的组件中,我确实喜欢这样:
ngOnInit(): void {
this.studyService.getSWAPI_List().subscribe(response => {
console.log(response)
}, error => {console.log(error)});
this.studyService.getSWAPI_List2().subscribe(response => {
console.log(response)
}, error => {console.log(error)});
}
为我服务:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor(private http: HttpClient) { }
getSWAPI_List(){
console.log('Request SWAPI list')
return this.http.get('https://swapi.dev/api/people/', options);
}
getSWAPI_List2(){
console.log('Request SWAPI list2')
return this.http.get('https://swapi.dev/api/people/');
}
}
并且在我的浏览器控制台中,我没有看到任何响应(对于有和没有 header 的请求)但是如果我在我的浏览器中使用这个简单的 URL 一切正常。 ..
我在控制台(以及控制台的网络选项卡)中也没有错误...
谁能帮我看看为什么?
代码乍一看似乎是正确的。
我的 Stackblitz example 也按预期工作。
控制台输出:
Request SWAPI list
Request SWAPI list2
Angular is running in development mode. Call enableProdMode() to enable production mode.
{count: 82, next: "http://swapi.dev/api/people/?page=2", previous: null, results: Array[10]…}
{count: 82, next: "http://swapi.dev/api/people/?page=2", previous: null, results: Array[10]…}
我建议您 post 更多信息或更新 Stackblitz 示例,以找到真正的问题。
您还可以下载 Stackblitz 示例
检查它是否适用于您的本地开发机器。
所以,我正在尝试向 API 发出请求(例如 SWAPI API)
在我的 app.module 中:
...
...
import { HttpClientModule } from '@angular/common/http';
declarations: [
AppComponent,
...
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientTestingModule,
HttpClientModule,
...
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在我的组件中,我确实喜欢这样:
ngOnInit(): void {
this.studyService.getSWAPI_List().subscribe(response => {
console.log(response)
}, error => {console.log(error)});
this.studyService.getSWAPI_List2().subscribe(response => {
console.log(response)
}, error => {console.log(error)});
}
为我服务:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor(private http: HttpClient) { }
getSWAPI_List(){
console.log('Request SWAPI list')
return this.http.get('https://swapi.dev/api/people/', options);
}
getSWAPI_List2(){
console.log('Request SWAPI list2')
return this.http.get('https://swapi.dev/api/people/');
}
}
并且在我的浏览器控制台中,我没有看到任何响应(对于有和没有 header 的请求)但是如果我在我的浏览器中使用这个简单的 URL 一切正常。 ..
我在控制台(以及控制台的网络选项卡)中也没有错误...
谁能帮我看看为什么?
代码乍一看似乎是正确的。
我的 Stackblitz example 也按预期工作。
控制台输出:
Request SWAPI list
Request SWAPI list2
Angular is running in development mode. Call enableProdMode() to enable production mode.
{count: 82, next: "http://swapi.dev/api/people/?page=2", previous: null, results: Array[10]…}
{count: 82, next: "http://swapi.dev/api/people/?page=2", previous: null, results: Array[10]…}
我建议您 post 更多信息或更新 Stackblitz 示例,以找到真正的问题。
您还可以下载 Stackblitz 示例
检查它是否适用于您的本地开发机器。