<script> // 第一步,创建ajax对象 var xhr = new XMLHttpRequest(); // 第二步,调用open()方法,配置请求方式和请求地址 xhr.open('POST', 'http://www.liulongbin.top:3006/api/addbook');; // 第三步,设置请求头,配置Content-type xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); // POST请求的参数格式 // 1、位置:放到send()方法中 // 2、格式:键值对字符串 // 第四步,发送请求 // xhr.send() xhr.send('bookname=米耀华博客&author=laoli&publisher=黑马'); // 第五步,监听onreadystatrchange事件 xhr.onreadystatechange = function () { // 第六步,判断请求是否成功 if (xhr.readyState === 4 && xhr.status === 200) { // 第七步,获取服务器响应的数据 console.log(xhr.responseText); } } </script>
正文
原生Ajax发送POST请求
转载请注明出处:米耀华博客
还没有评论,来说两句吧...