We码小程序JSAPI发起网络请求

举报
云会议运营喵大人 发表于 2020/02/13 19:11:20 2020/02/13
【摘要】 HWH5.fetchInternet支持版本>=10.0.2扫码体验:发起网络请求请求参数参数类型必填说明urlString是接口url。提示:URL中如果包含中文或者特殊字符,请使用encodeURIComponent进行处理methodString是服务请求类型,仅支持 get / post / put / delete。注:请求类型必须是小写bodyString是请求参数headers...

HWH5.fetchInternet

支持版本>=10.0.2

扫码体验:

1581592179122213.png


发起网络请求

请求参数

参数类型必填说明
urlString接口url。提示:URL中如果包含中文或者特殊字符,请使用encodeURIComponent进行处理
methodString服务请求类型,仅支持 get / post / put / delete。注:请求类型必须是小写
bodyString请求参数
headersObject请求头
timeoutNumber超时时间

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

返回结果

无。

请求示例

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);
    });


【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。