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

被折叠的 条评论
为什么被折叠?



