Home > SYS_ClientUrl > request
SYS_ClientUrl.request() method
发起即时请求
签名
typescript
function request(
url: string,
method?: 'GET' | 'POST' | 'HEAD' | 'PUT' | 'DELETE' | 'PATCH',
data?: string | Blob | FormData | URLSearchParams,
options?: { headers?: undefined | { [key: string]: any }; integrity?: undefined | string },
succeedCallFn?: (data: Response) => void | Promise<void>,
): Promise<Response>;1
2
3
4
5
6
7
2
3
4
5
6
7
参数名
参数 | 类型 | 描述 |
|---|---|---|
url | string | 请求地址 |
method | 'GET' | 'POST' | 'HEAD' | 'PUT' | 'DELETE' | 'PATCH' | (可选) 请求方法 |
data | string | Blob | FormData | URLSearchParams | (可选) 请求发送的数据,可以是直接数据或 URLSearchParams 对象,如果 method 为 |
options | { headers?: undefined | { [key: string]: any }; integrity?: undefined | string } | (可选) 请求参数 |
succeedCallFn | (data: Response) => void | Promise<void> | (可选) 请求成功后回调的函数 |
返回值
Promise<Response>
Fetch 的返回结果
备注
请注意,需要在被请求的站点上允许跨源资源共享(CORS),否则接口将始终返回错误结果。
更多信息,请查阅 跨源资源共享 (CORS) - MDN。
注意:本接口需要使用者启用扩展的外部交互权限,如若未启用将始终 throw Error
示例
javascript
// 1. 发起 GET 请求(本例用本机桥接地址演示,实际使用时换成自己的服务地址)
const response = await eda.sys_ClientUrl.request('http://localhost:49620/health', 'GET');
// 2. 读取响应状态码
console.log('状态码:', response.status);
// 3. 读取响应内容(Response 是标准 fetch 返回值,用法与浏览器一致)
const body = await response.text();
console.log('响应内容:', body);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9