HWH5.fetchInternet

支持版本>=10.0.2

扫码体验:

vscode-plugin

发起网络请求

请求参数

参数 类型 必填 说明
url String 接口url。提示:URL中如果包含中文或者特殊字符,请使用encodeURIComponent进行处理
method String 服务请求类型,仅支持 get / post / put / delete
body String 请求参数
headers Object 请求头
timeout Number 超时时间

注意:headers为x-www-form-urlencoded时,body需要code=value&key=value格式

返回结果

无。

注意:对于服务返回为json格式的数据,使用json方法;对于服务返回为纯文本非json格式的,使用text方法,严格区分

请求示例

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);
    });
    
  • form提交示例

    const _url = 'http://www.xxx.com/xxxx';
    const _headers = {
      'Content-Type': 'application/x-www-form-urlencoded'
    };
    
    HWH5.fetchInternet(_url, {
      method: 'post',
      body: 'param1=xxx&param2=xxx',
      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);
    });
    

结果。 ""

    无结果。 ""