js原生的ajax

it2025-10-22  6

function ajaxDom(){  // 创建ajax对象  var xhr = new XMLHttpRequest();    //监听ajax的状态 到哪一步  xhr.onreadystatechange = function(){    if(xhr.readyState == 4){           alert(xhr.responseText);    //数据处理    }   }   //发送请求  xhr.open('get','index.php?id=1');       //用get传值的方式  xhr.open('post','index.php');    //用post传值的方式   //发送数据  get写null    post写键值对  xhr.send(null);    xhr.setRequestHeader('content-type','application/x-www-form-urlencode');  var data = "id=1&type=add";  xhr.send(data); }

转载于:https://www.cnblogs.com/tanchangsha/p/10195413.html

相关资源:Javascript原生ajax请求代码实例
最新回复(0)