XMLHttpRequest 对象使用案例

来源:互联网 发布:淘宝在国外的影响力 编辑:程序博客网 时间:2024/06/01 19:47
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);