浅谈JS原生Ajax,GET和POST
作者:jingxian 时间:2024-04-16 08:52:41
javascript/js的ajax的GET请求:
<script type="text/javascript">
/* 创建 XMLHttpRequest 对象 */
var xmlHttp;
function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
// -----------ajax方法-----------//
function getLabelsGet(){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert('您的浏览器不支持AJAX!');
return;
}
var id = document.getElementById('id').value;
var url="http://www.Leefrom.com?id="+id+"&t/"+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=favorOK;//发送事件后,收到信息了调用函数
xmlHttp.send();
}
function getOkGet(){
if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){
// 本地提示:加载中
}
if (xmlHttp.readyState==4 && xmlHttp.status==200){
var d= xmlHttp.responseText;
// 处理返回结果
}
}
</script>
javascript/js的ajax的POST请求:
<script type="text/javascript">
/* 创建 XMLHttpRequest 对象 */
var xmlHttp;
function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
// -----------ajax方法-----------//
function getLabelsPost(){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert('您的浏览器不支持AJAX!');
return;
}
var url="http://www.lifefrom.com/t/"+Math.random();
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
xmlHttp.onreadystatechange=getLabelsOK;//发送事件后,收到信息了调用函数
}
function getOkPost(){
if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){
// 本地提示:加载中/处理中
}
if (xmlHttp.readyState==4 && xmlHttp.status==200){
var d=xmlHttp.responseText; // 返回值
// 处理返回值
}
}
</script>
注意:XMLHttpRequest 是 AJAX 的基础,在创建 XMLHttpRequest 对象时,必须与你写的ajax方法在同一个‘<script></script>'标签中!否则ajax请求会出错,并无法返回数据。 javascript/js的ajax的POST/GET请求。
标签:原生js,ajax,post
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Python中的同步异步阻塞与非阻塞
2021-09-03 05:54:01
python获得文件创建时间和修改时间的方法
2023-10-12 05:22:50
python广度搜索解决八数码难题
2023-01-26 18:12:43
实例分析python3实现并发访问水平切分表
2023-12-29 06:43:45
pandas 如何分割字符的实现方法
2021-04-17 11:28:46
![](https://img.aspxhome.com/file/2023/9/90759_0s.jpg)
python绘制分组对比柱状图
2021-01-03 06:48:26
![](https://img.aspxhome.com/file/2023/5/97335_0s.jpg)
将MySQL的作为文件系统使用
2011-12-14 18:34:26
Go语言开发发送Get和Post请求的示例
2024-05-21 10:23:46
windows安装python超详细图文教程
2023-08-01 15:19:32
![](https://img.aspxhome.com/file/2023/2/61762_0s.png)
python统计一个文本中重复行数的方法
2021-08-20 12:50:46
Apple在Safari 4中抛弃品牌视觉设计
2009-02-26 13:05:00
![](https://img.aspxhome.com/file/UploadPic/20092/26/safari_4-60s.png)
选项卡动态增删的效果(内嵌框架)
2008-05-22 12:59:00
![](https://img.aspxhome.com/file/UploadPic/20085/22/asgard_cardprogram_62s.jpg)
Python 语言实现六大查找算法
2022-11-09 14:41:30
![](https://img.aspxhome.com/file/2023/0/124650_0s.png)
Finished with error:Navicat运行SQL文件报错的解决
2024-01-28 01:40:03
![](https://img.aspxhome.com/file/2023/2/124822_0s.png)
opencv python 基于KNN的手写体识别的实例
2021-02-22 13:03:02
MySQL单表查询进阶教程(最全面!)
2024-01-22 21:06:42
![](https://img.aspxhome.com/file/2023/7/68127_0s.png)
LyScript实现Hook改写MessageBox的方法详解
2023-01-07 03:35:16
![](https://img.aspxhome.com/file/2023/0/107950_0s.png)
document.all与WEB标准
2008-03-16 14:28:00
mysql清空表数据的两种方式和区别解析
2024-01-18 09:40:40
MSSQL SERVER 2005 数学函数整理
2024-01-20 23:37:17