发起网络请求


接口说明

支持版本>=10.0.2

返回值必须为JSON格式。

HWH5.fetchInternet

请求参数

参数 类型 必填 说明
url String 接口url
method String 服务请求类型,仅支持 get / post / put / delete。注:请求类型必须是小写
body String 请求参数
headers Object 请求头
timeout Number 超时时间

返回结果

无。

请求示例

get请求

  • ES6版本

    const url = 'http://www.example.com/api';
    
    const _headers = {
      'Content-Type': 'application/json'
    };
    
    HWH5.fetchInternet(url, { method: 'get', headers: _headers, timeout: 6000 }).then(res => {
      res.json().then(reply => {
        console.log('服务端返回: ', reply);
      });
    }).catch(error => {
      console.log('请求异常', error);
    });
    
  • ES5版本

    var url = 'http://www.example.com/api';
    
    var _headers = {
      'Content-Type': 'application/json'
    };
    
    HWH5.fetchInternet(url, { method: 'get', headers: _headers, timeout: 6000 }).then(function(res) {
      res.json().then(function (reply) {
        console.log('服务端返回: ', reply);
      });
    }).catch(function (error) {
      console.log('请求异常', error);
    });
    

post请求

  • ES6版本

    const _url = 'http://www.xxx.com/xxxx';
    const _headers = {
      'Content-Type': 'application/json'
    };
    
    const _params = {
      param1: 'xxx',
      param2: 'xxx'
    };
    
    HWH5.fetchInternet(_url, {
      method: 'post',
      body: JSON.stringify(_params),
      headers: _headers
    }).then(res => {
      res.json().then(reply => {
        console.log('服务端返回: ', reply);
      });
    }).catch(error => {
      console.log('请求异常', error);
    });
    
  • ES5版本

    var _url = 'http://www.xxx.com/xxxx';
    
    var _headers = {
      'Content-Type': 'application/json'
    };
    
    var _params = {
      param1: 'xxx',
      param2: 'xxx'
    };
    
    HWH5.fetchInternet(_url, {
      method: 'post',
      body: JSON.stringify(_params),
      headers: _headers
    }).then(function (res) {
      res.json().then(function (reply) {
        console.log('服务端返回: ', reply);
      });
    }).catch(function (error) {
      console.log('请求异常', error);
    });
    

result. ""

    Not Found. ""