博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AJAX - 向服务器发送请求
阅读量:4654 次
发布时间:2019-06-09

本文共 3044 字,大约阅读时间需要 10 分钟。

向服务器发送请求如需将请求发送到服务器,

我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:

method:请求的类型;GET 或 POST

url:文件在服务器上的位置

async:true(异步)或 false(同步)

send(string)

string:仅用于 POST 请求

GET请求

一个简单的get请求:

 

<html>

<head>
<script type="text/javascript">
function loadXMLDoc()
{
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","/ajax/demo_get.asp",true);
xmlhttp.send();
}
</script>
</head>
<body>

 

<h2>AJAX</h2>

<button type="button" οnclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>

 

</body>

</html>

在上面的例子中,您可能得到的是缓存的结果。

为了避免这种情况,请向 URL 添加一个唯一的 ID:

<html>

<head>
<script type="text/javascript">
function loadXMLDoc()
{
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","/ajax/demo_get.asp?t=" + Math.random(),true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>

<button type="button" οnclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>

</body>

</html>

POST 请求

一个简单 POST 请求:

<html>

<head>
<script type="text/javascript">
function loadXMLDoc()
{
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("POST","/ajax/demo_post.asp",true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>

<button type="button" οnclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>

如果需要像 HTML 表单那样 POST 数据,请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定希望发送的数据:

<html>

<head>
<script type="text/javascript">
function loadXMLDoc()
{
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("POST","/ajax/demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");
}
</script>
</head>
<body>

<h2>AJAX</h2>

<button type="button" οnclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>

 

转载于:https://www.cnblogs.com/dongtian1992/p/7505495.html

你可能感兴趣的文章
多线程(二)--NSThread基本使用
查看>>
git command
查看>>
使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍
查看>>
html里 调整字间距
查看>>
RabbitMQ的Vhost,Exchange,Queue原理分析
查看>>
Mac上编写C语言程序
查看>>
251.Flatten 2D Vector
查看>>
WLS Exception: Need to specify class name in environment or system property Solution
查看>>
人见人爱A^B
查看>>
android游戏开发框架libgdx的使用(一)--环境搭建
查看>>
【转】wait_fences: failed to receive reply: 10004003问题的引起原因
查看>>
PE制作实录 —— 定义我的 PE 工具箱
查看>>
C++优先队列的重载(最小堆、最大堆)
查看>>
【读书笔记】理解基本排序算法
查看>>
消除头文件
查看>>
Android中数据文件解析(Json解析)
查看>>
自定义seekBar设置进度条背景图片
查看>>
java容器类1:Collection,List,ArrayList,LinkedList深入解读
查看>>
16日彻底去除安卓应用的内置广告
查看>>
再谈.NET Micro Framework移植
查看>>