ajax如何实现异步的,如何实现AJAX的异步功能

本文给大家分享的是如何实现AJAX的异步功能,非常的详细,也很实用,适合JavaScript的初学者,有需要的小伙伴参考下。

关于AJAX异步功能的小实验

为了实验ajax的异步

性,先建一个Web项目,结构大概是这个样子

dea44fcece5d82a46202d4206af81561.png

TestServlet.java(主要是提供ajax后台调用的程序)import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class TestServelet

*/

@WebServlet("/TestServelet")

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* Default constructor.

*/

public TestServlet() {

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//为了体现程序的异步性,先让它睡3s

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

response.setCharacterEncoding("utf-8");

//打印出程序运行的时间

System.out.println("异步程序运行时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS") .format(new Date()));

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub

doGet(request, response);

}

}

testAjax.jsp(前台页面和js,触发异步调用)

Hello Ajax!

function testajax(){

fnGetAjaxReturn('http://localhost:8080/TestProject/TestServlet?a='+Math.random());

}

function fnGetAjaxReturn(url)

{

var userAgent = navigator.userAgent;

var http_request = false;

//开始初始化XMLHttpRequest对象

if(window.XMLHttpRequest) { //Mozilla 浏览器

http_request = new XMLHttpRequest();

if (http_request.overrideMimeType) {//设置MiME类别

http_request.overrideMimeType("text/xml");

}

}

//else if (window.ActiveXObject) { // IE浏览器

else if (window.ActiveXObject||userAgent.indexOf("Trident") > -1){

try {

http_request = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) { try {

http_request = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {alert("错了吧");}

}

} if (!http_request) { // 异常,创建对象实例失败

window.alert("不能创建XMLHttpRequest对象实例.");

return false;

}

http_request.open("GET", url, true);//true 异步 false 同步

http_request.send();

alert("异步请求之后执行时间:"+new Date +'\n毫秒数:'+ new Date().getMilliseconds());

}

说明:jsp页面加载之后调用一个js,js中会先发出一个异步请求,再执行一个alert弹出操作。

下面开始试验

用IE浏览器,运行URL

http://localhost:8080/TestProject/testAjax.jsp

发现,程序先弹出了alert,过了3s钟,ajax才返回了后台结果,充分证明了ajax的异步性。

运行结果:

f2276a176fa4dd4b360fddfe39fa8c78.png

从时间的差异性,发现程序先执行结束(执行了alert),过了3s钟,异步程序才返回结果。

反过来,如果把jsp文件中的http_request.open("GET", url, true);

true 改为 false 呢?

运行URL,发现程序在苦苦等待Servlet后台请求返回之后,才执行了js中的最后一段代码alert,正是所谓的同步调用。

运行结果如下:

3482f6961c8b52ae571b76be5f9fd04e.png

The end!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值