ajax(一)
发布时间:2020-12-15 21:31:55 所属栏目:百科 来源:网络整理
导读:创建xhr对象: new XHRHttpRequest() xhr对象的属性: responseText: 代表响应主体的响应文本; status: 返回状态码,200: ok,404:错误… 同步方式 xhr.open( 'get' , 'test.php?rand=Math.random()' , false ); // false:同步 xhr.send( null ); 异步方式 onrea
xhr.open('get','test.php?rand=Math.random()',false); // false:同步
xhr.send(null);
异步方式
xhr.onreadstatechange = function(){
if(xhr.readyState==4 && xhr.status==200){
console.log(xhr.responseText);
}
}
xhr.open('get','test.php',true);
xhr.send(null);
GET
Post
xhr.onreadstatechange = function(){
if(xhr.readyState==4 && xhr.status==200){
console.log(xhr.responseText);
}
}
xhr.open('post',true);
// 修改请求头,模拟表单提交
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send('name=Lee&age=10'); // 通过这里提交数据
头信息
特殊字符编码
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |