离子本机 HTTP return 未定义
Ionic native HTTP return Undefined
我正在尝试从 angular http 迁移到 ionic native http
The plugin: cordova-plugin-advanced-http
这是 get 调用的代码:
let response = from(this.http.post(url, {body}, {httpOptions})
.then(res => { return JSON.parse(res.data) }, res => console.log(res.data)));
return response;
这是我尝试过的另一种方式:
let promise = this.http.get(url, {}, {httpOptions})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
return JSON.parse(data.data);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
return from(promise);
The return and the console log values are always undefined.
在此之前,我使用 angular http 进行了此操作,并且效果很好。
像这样:
return this.http
.post(url, body, httpOptions);
问题是什么?请任何解决方案!
import { HttpClient } from '@angular/common/http';
import { HTTP } from '@ionic-native/http';
...
private headers: any = {};
constructor
(
public http: HttpClient,
private Http: HTTP,
){
this.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
};
this.Http.setRequestTimeout(10.0);
}
...
async httpPost( url? , data? ):Promise<any>{
let self = this;
return new Promise( (resolve) => {
self.Http.post( url , data, self.headers )
.then(res => {
console.log(res);
var response = JSON.parse(res.data);
resolve(response);
})
.catch(error => {
resolve(error);
});
});
}
我正在尝试从 angular http 迁移到 ionic native http
The plugin: cordova-plugin-advanced-http
这是 get 调用的代码:
let response = from(this.http.post(url, {body}, {httpOptions})
.then(res => { return JSON.parse(res.data) }, res => console.log(res.data)));
return response;
这是我尝试过的另一种方式:
let promise = this.http.get(url, {}, {httpOptions})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
return JSON.parse(data.data);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
return from(promise);
The return and the console log values are always undefined.
在此之前,我使用 angular http 进行了此操作,并且效果很好。 像这样:
return this.http
.post(url, body, httpOptions);
问题是什么?请任何解决方案!
import { HttpClient } from '@angular/common/http';
import { HTTP } from '@ionic-native/http';
...
private headers: any = {};
constructor
(
public http: HttpClient,
private Http: HTTP,
){
this.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
};
this.Http.setRequestTimeout(10.0);
}
...
async httpPost( url? , data? ):Promise<any>{
let self = this;
return new Promise( (resolve) => {
self.Http.post( url , data, self.headers )
.then(res => {
console.log(res);
var response = JSON.parse(res.data);
resolve(response);
})
.catch(error => {
resolve(error);
});
});
}