效果
js
//获取图文验证码
function getImgCode(){
var xhr = new XMLHttpRequest();
xhr.open('POST', 'getImgCode.action', true);
xhr.setRequestHeader("Accept", "application/json;q=0.5");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.responseType = "blob"; // 返回类型blob
// 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
xhr.onload = function () {
// 请求完成
if (this.status === 200) {
var blob = this.response;
var reader = new FileReader();
reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
reader.onload = function () {
$("#getimg").attr("src", reader.result);
}
}
};
// 发送ajax请求
xhr.send();
}