var formData = new FormData();
formData.append('username', 'johndoe');
formData.append('id', 123456);
//创建xhr对象
var xhr = new XMLHttpRequest();
//设置xhr请求的超时时间
xhr.timeout = 3000;
//设置响应返回的数据格式
xhr.responseType = "text";
//创建一个 post 请求,采用异步
xhr.open('POST', url, true);
//注册相关事件回调处理函数
xhr.onload = function(e) {
if(this.status == 200||this.status == 304){
alert(this.responseText);
}
};
xhr.ontimeout = function(e) { alert(1); };
xhr.onerror = function(e) { alert(2)};
xhr.upload.onprogress = function(e) { alert(3); };
//发送数据
xhr.send(formData);
XMLHttpRequest 对象使用案例
最新推荐文章于 2025-02-09 19:14:16 发布