开发 API 网络 发起请求 ks.request
# ks.request
更新时间:2024-12-09 11:57:47
发起 HTTPS 网络请求。
使用前请注意阅读相关说明 (opens new window)。
# 参数
# Object object
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| url | string | 是 | 开发者服务器接口地址 | |
| data | string/object | 否 | 请求的参数 | |
| header | Object | 否 | 设置请求的 header,header 中不能设置 Referer。 content-type 默认为 application/json | |
| timeout | number | 全局配置 (opens new window) or 60000ms | 否 | 超时时间,单位为毫秒 |
| method | string | GET | 否 | HTTP 请求方法 |
| dataType | string | json | 否 | 返回的数据格式 |
| responseType | string | text | 否 | 响应的数据类型 |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
# method 的合法值
| 值 | 说明 |
|---|---|
| GET | HTTP 请求 GET |
| POST | HTTP 请求 POST |
# dataType 的合法值
| 值 | 说明 |
|---|---|
| json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse |
| 其他 | 不对返回的内容进行 JSON.parse |
# responseType 的合法值
| 值 | 说明 |
|---|---|
| text | 响应的数据为文本 |
# data转换说明
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
- 对于
GET方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...) - 对于
POST方法且header['content-type']为application/json的数据,会对数据进行 JSON 序列化 - 对于
POST方法且header['content-type']为application/x-www-form-urlencoded的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
# success 回调参数
# Object res
| 属性 | 类型 | 说明 |
|---|---|---|
| data | string/Object/Arraybuffer | 开发者服务器返回的数据 |
| statusCode | number | 开发者服务器返回的 HTTP 状态码 |
| header | Object | 开发者服务器返回的 HTTP Response Header |
| cookies | Array | 开发者服务器返回的 cookies,格式为字符串数组 |
# 返回值
# RequestTask
请求任务对象
# 错误信息
| 错误信息 | 错误说明 |
|---|---|
| data is too big | 返回数据size超限 |
| request has up to max count | 同时请求数量超限制 |
| host/url not in domain list | host/url 不在白名单内 |
| invalid url | 非法url |
# 示例代码
ks.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success(res) {
console.log(res.data)
}
})
上一篇:《ks.offWindowResize》
下一篇:《RequestTask》
仍有疑问? 前往社区提问