提供网络相关API
request
请求网络
参数类型:
{
url?: string;
method?: "get" | "post" | "put" | "delete";
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
data?: object | string;
timeout?: number;
}返回值:
Promise< {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}>使用示例:
let r: IRequest = {
url: "http://baobab.kaiyanapp.com/api/v4/discovery/hot",
method: "post",
data: "start=1&num=1"
};
network(this.context).request(r).then((res) => {
const jsonStr = JSON.stringify(res);
}).catch((e) => {
modal(this.context).toast("Catched:" + JSON.stringify(e));
});
get
GET 请求
参数:
url,
{
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
timeout?: number;
}返回值:
Promise< {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}>使用示例:
network(this.context).get('https://www.dmoe.cc/random.php?return=json').then((res) => {
const jsonStr = JSON.stringify(res);
}).catch((e) => {
modal(this.context).toast("Catched:" + JSON.stringify(e));
});
post
POST请求
参数:
url,
data?: string | object | undefined,
{
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
timeout?: number;
}返回值:
Promise< {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}>使用示例:
const data = 'start=1&num=1'
network(this.context).post('http://baobab.kaiyanapp.com/api/v4/discovery/hot', data).then((res) => {
const jsonStr = JSON.stringify(res);
}).catch((e) => {
modal(this.context).toast("Catched:" + JSON.stringify(e));
});
put
PUT请求
- 参数:
url,
data?: string | object | undefined,
{
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
timeout?: number;
} - 返回值:
Promise< {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}>
delete
DELETE请求
- 参数:
url,
data?: string | object | undefined,
{
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
timeout?: number;
} - 返回值:
Promise< {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}>