//获取校验码
function getImg() {
var windowUrl = window.URL || window.webkitURL; //处理浏览器兼容性
var xhr = new XMLHttpRequest();
var url = ''; //验证码请求地址
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
var blob = this.response;
$("#code").attr("src",windowUrl.createObjectURL(blob));
}
};
xhr.send();
}
通过接口验证session保持一致,需要在ajax请求中加xhrFields
$.ajax({
url:",
type:"post",
xhrFields:{
withCredentials:true
},
success:function(data){
}
});
博客展示了使用JavaScript获取校验码的代码,通过XMLHttpRequest对象发送GET请求获取验证码图片。同时提到在AJAX请求中,为保持接口验证时session一致,需在请求里添加xhrFields并设置withCredentials为true。
545

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



