ionic httpClient 请求错误?
ionic httpClient does wrong request?
所以我试图对我的 API 进行快速测试,但我遇到了这个奇怪的问题...
这是我的 ApiService 代码:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class ApiService {
constructor(private http: HttpClient) { }
async getData(url, method): Promise<Object>{
let fullurl = url + method
console.log(fullurl);
return await new Promise(resolve=>{
this.http.get(fullurl)
.subscribe(data=>{
resolve(data);
return data;
},error=>{
console.log(error);
});
});
}
}
我是这样称呼我的服务的:
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../services/api.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
providers:[ApiService]
})
export class HomePage implements OnInit {
constructor( private api:ApiService,) {}
url:string="api.test.tk/"
method:string="SangreDragon"
json:any
async ngOnInit(){
console.log(this.url+ this.method);
this.json = await this.api.getData(this.url, this.method);
await console.log(this.json);
}
}
GET 调用以某种方式转到:http://localhost:8100/api.test.tk/SangreDragon
我正在使用 Ionic 服务来测试这个...我应该编译来测试它吗?
为什么?
显然...如果您不将 http:// 放在 URL 上,它会尝试转到本地主机...所以它是固定的。我想感谢您的阅读。
所以我试图对我的 API 进行快速测试,但我遇到了这个奇怪的问题...
这是我的 ApiService 代码:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class ApiService {
constructor(private http: HttpClient) { }
async getData(url, method): Promise<Object>{
let fullurl = url + method
console.log(fullurl);
return await new Promise(resolve=>{
this.http.get(fullurl)
.subscribe(data=>{
resolve(data);
return data;
},error=>{
console.log(error);
});
});
}
}
我是这样称呼我的服务的:
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../services/api.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
providers:[ApiService]
})
export class HomePage implements OnInit {
constructor( private api:ApiService,) {}
url:string="api.test.tk/"
method:string="SangreDragon"
json:any
async ngOnInit(){
console.log(this.url+ this.method);
this.json = await this.api.getData(this.url, this.method);
await console.log(this.json);
}
}
GET 调用以某种方式转到:http://localhost:8100/api.test.tk/SangreDragon
我正在使用 Ionic 服务来测试这个...我应该编译来测试它吗?
为什么?
显然...如果您不将 http:// 放在 URL 上,它会尝试转到本地主机...所以它是固定的。我想感谢您的阅读。