1、 uni.request(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 数据说明
    最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:

    1. 对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 }
    2. 转换后的结果是 name=name&age=18
    3. 对于 POST 方法且 header['content-type'] application/json 的数据,会进行 JSON 序列化。
    4. 对于 POST 方法且 header['content-type'] application/x-www-form-urlencoded 的数据,会将数据转换为 query string

    示例:

    1. <template>
    2. <view></view>
    3. </template>
    4. <script>
    5. export default {
    6. data:{
    7. },
    8. onLoad:function(){
    9. //get
    10. const requestTask1 = uni.request({
    11. url: 'https://demo.hcoder.net',
    12. success: function (res) {
    13. console.log(res.data);
    14. }
    15. });
    16. //
    17. const requestTask2 = uni.request({
    18. url: 'https://demo.hcoder.net/index.php?m=getJson',
    19. success: function (res) {
    20. console.log(res.data);
    21. }
    22. });
    23. //
    24. const requestTask3 = uni.request({
    25. url: 'https://demo.hcoder.net/index.php',
    26. data: {name : 'hcoder...', 'age' : 18},
    27. method:"POST",
    28. header : {'content-type':'application/x-www-form-urlencoded'},
    29. success: function (res) {
    30. console.log(res.data);
    31. }
    32. });
    33. }
    34. }
    35. </script>