原生态ajax请求怎么写
程序员文章站
2022-06-10 17:22:27
...
//第一步:创建XMLHttpRequest对象
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//第二步:设置回调函数
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
//第三步:打开连接
xmlhttp.open("GET","test1.txt",true);
//第四步:发送请求
//post请求的时候,需要设置requestHearder属性。两个参数,第一个参数表示头名称,第二个表示头的值
//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//然后再send中传入请求的参数。如:"fname=Bill&lname=Gates"
xmlhttp.send();
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//第二步:设置回调函数
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
//第三步:打开连接
xmlhttp.open("GET","test1.txt",true);
//第四步:发送请求
//post请求的时候,需要设置requestHearder属性。两个参数,第一个参数表示头名称,第二个表示头的值
//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//然后再send中传入请求的参数。如:"fname=Bill&lname=Gates"
xmlhttp.send();
下一篇: 各种快速幂(qaq)
推荐阅读
-
遇到项目RESTful改造时怎么用ajax的$post方法请求api接口?
-
原生JS写Ajax的请求函数功能
-
axios进阶实践之利用最优雅的方式写ajax请求
-
jQuery进阶实践之利用最优雅的方式如何写ajax请求
-
javascript - 微信公众号,前台向后台发起ajax请求,后台获取不到发送过来的值是怎么回事??
-
微信公众号页面中怎么发起ajax请求??
-
javascript - 一个页面中有多个相同的form,分别去发起异步请求,后写的$.ajax方法,数据发送不出去,控制台也没有报错?
-
做web 推送 怎么隐藏这个ajax请求
-
Ajax怎么操作post请求跳转页面
-
使用Ajax请求的数据为何保存不到数据库中?后台用php写,各位大神支个招