XMLHttpRequest请求数据与Request请求数据写法

本文介绍了通过Ajax和Request两种方式实现数据获取的方法,并提供了具体的示例代码。其中包括了使用XMLHttpRequest发送Ajax请求及处理响应的过程,同时也展示了利用Request进行GET请求的方式。

XMLHttpRequest发送Ajax请求的简单示例代码:

let xhr;
if (window.XMLHttpRequest) {
   xhr = new XMLHttpRequest();
} else {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.responseType = "arraybuffer"; 
xhr.open("GET", " /getByIds?ids="+ this.rgbImagePath, true);
xhr.onload = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
       //xhr.response为请求到的数据
       console.log('xhr.response:',xhr.response);
     }
 };
xhr.send();

Request请求的简单实例代码:

request({
    url:"/getByIds?ids="+this.rgbImagePath,
    methods:"get",
   }).then((res)=>{
     this.imageArray = res;
   })

查询字符串写法:

//不带参数的URL地址
“…/…/getByIds”
//带一个参数的URL地址
“…/…/getByIds?ids”+this.id
//带两个参数的URL地址
“…/…/getByIds?ids”+this.id+“&name=”+this.name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值