uni.request(OBJECT)
发起网络请求。json
OBJECT 参数说明服务器
参数名 类型 必填 默认值 说明
url String 是 开发者服务器接口地址
data Object/String/ArrayBuffer 否 请求的参数
header Object 否 设置请求的 header,header 中不能设置 Referer。
method String 否 GET (需大写)有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
dataType String 否 json 若是设为 json,会尝试对返回的数据作一次 JSON.parse
responseType String 否 text 设置响应的数据类型。合法值:text、arraybuffer
success Function 否 收到开发者服务成功返回的回调函数
fail Function 否 接口调用失败的回调函数
complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
success 返回参数说明网络
参数 类型 说明
data Object/String/ArrayBuffer 开发者服务器返回的数据
statusCode Number 开发者服务器返回的 HTTP 状态码
header Object 开发者服务器返回的 HTTP Response Header
data 数听说明app
最终发送给服务器的数据是 String 类型,若是传入的 data 不是 String 类型,会被转换成 String。转换规则以下:函数
对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。
对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。
对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。
示例url
uni.request({code
url: 'https://www.example.com/request', //仅为示例,并不是真实接口地址。 data: { name: 'name', age: 18 }, header: { 'custom-header': 'hello' //自定义请求头信息 }, success: function (res) { console.log(res.data); }
});
返回值orm
返回一个 requestTask 对象,经过 requestTask,可中断请求任务。对象
requestTask 对象的方法列表接口
方法 参数 说明
abort 中断请求任务
示例
const requestTask = uni.request({
url: 'https://www.example.com/request', //仅为示例,并不是真实接口地址。 data: { name: 'name', age: 18 }, success: function (res) { console.log(res.data); }
});
// 中断请求任务
requestTask.abort();
Tips
请求的 header 中 content-type 默认为 application/json。