Ajax实践之用户是否存在

        关于Ajax在之前的学习中,已经对它的基础知识有了初步的了解。仅仅是欠实践。

那么接下来就让实践来检验一下真理吧!

        基础见:http://blog.csdn.net/liu_yujie2011com/article/details/29812777

        那么先回忆一下,Ajax是用来解决什么问题的?答案非常easy:解决同步和异步的问题。从而提高界面的友好型。增强用户体验。那么就结合“在加入用户时推断用户是否存在”的实例来体验一下吧。

一、HTMLinput代码

<tdwidth="78%">
<inputname="userId" type="text" class="text1"id="userId"
size="10"maxlength="10" οnkeypress="userIdOnKeyPress()"value="<%=userId%>" οnblur="validate(this)">
<spanid="spanUserId"></span>
</td>

二、Jsp界面代码

<%@ pagelanguage="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<%@ pageimport="com.bjpowernode.drp.sysmgr.manager.*" %> 
<%
StringuserId=request.getParameter("userId");
if(UserManager.getInstance().findUserById(userId) != null) {
out.println("用户代码已经存在");
}%>                

三、Javascript代码

(一)创建Ajax引擎对象XMLHttpRequest

    var xmlHttp;
functioncreateXMLHttpRequest() {
//表示当前浏览器不是ie,如ns,firefox
if(window.XMLHttpRequest){
xmlHttp= new XMLHttpRequest();
}else if (window.ActiveXObject) {
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
}

(二)调用open方法与Ajax引擎建立连接,并告诉Ajax引擎我们的请求方式为get,请求url及採用异步方式

functionvalidate(field){
//alert(document.getElementById("userId").value);
//alert(field.value);
if(trim(document.getElementById("userId").value).length!= 0){
//创建Ajax核心对象XMLHttpRequest
createXMLHttpRequest();//解决缓存方法,用毫秒
varurl="user_validate.jsp?userId=" +trim(document.getElementById("userId").value)+ "&time="+new Date().getTime();//设置请求方式用GET,设置请求的URL。设置异步提交
xmlHttp.open("GET",url,true);//将方法地址复制给onreadystatechange属性
//相似于电话号码
xmlHttp.onreadystatechange=callback;//将设置的信息发送到Ajax引擎
xmlHttp.send(null);}else {
document.getElementById("spanUserId").innerHTML= "";
}
}

(三)告诉Ajax引擎处理完后。我们通常指定一个方法句柄,从而Ajax就会调用我们指定的方法。这样就能够得到Ajax引擎返回的数据(回调机制)。指定的方法例如以下:

function callback(){
//Ajax引擎状态为成功
if(xmlHttp.readyState==4){
//http协议状态为成功
if(xmlHttp.status==200){
//alert("请求成功!

") //推断假设为空,将红字span去掉 if(trim(xmlHttp.responseText) != ""){ document.getElementById("spanUserId").innerHTML= "<font color='red'>" + xmlHttp.responseText +"</font>" }else{ document.getElementById("spanUserId").innerHTML= ""; } }else{ alert("请求失败,错误码="+xmlHttp.status); } } }

(四)最后调用send方法把我们步骤设置的參数发给Ajax引擎去处理。这里指的就是步骤二的“xmlHttp.send(null)

四、注意

         以上就是对Ajax的一个简单实现,但在这里还要注意几点:

(一)使用Ajax技术须要清除缓存。否则easy产生莫名其妙的错误,详细的方法有两种:

1.採用URL后跟上时间戳(产生随机数)来防止浏览器缓存,如:

//解决缓存方法,用毫秒

varurl="user_validate.jsp?userId=" +trim(document.getElementById("userId").value)+ "&time="+new Date().getTime();

2.增加清除缓存代码,如:

header("Cache-Control:no-cache, must-revalidate"); 
xmlHttp.setRequestHeader("If-Modified-Since","0"); 
xmlHttp.setRequestHeader("Cache-Control","no-cache");

(二)假设须要提起多个请求,须要创建多个XMLHttpRequest对象。

五、总结

        通过以上的步骤大家基本上了解了Ajax实践的基本流程,那么接下来就要通过使用JS中的匿名函数来完毕推断用户时候存在的代码,期待下一篇吧!

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543818.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vb 导出整数 科学计数法_可整数组的计数

vb 导出整数 科学计数法Problem statement: 问题陈述&#xff1a; Given two positive integer n and m, find how many arrays of size n that can be formed such that: 给定两个正整数n和m &#xff0c;找出可以形成多少个大小为n的数组&#xff1a; Each element of the …

C4.5决策树算法概念学习

数据挖掘一般是指从大量的数据中自动搜索隐藏于其中的有着特殊关系性的信息的过程。 •分类和聚类•分类(Classification)就是按照某种标准给对象贴标签&#xff0c;再根据标签来区分归类&#xff0c;类别数不变。•聚类(clustering)是指根据“物以类聚”的原理&#xff0c;将本…

python修改y轴刻度_Python | Y轴刻度限制

python修改y轴刻度In some cases, we need to visualize our data within some defined range rather than the whole data. For this, we generally set the y-axis scale within a limit and this ultimately helps us to visualize better. Sometimes, it acts as zooming a…

em算法示例_带有示例HTML'em'标签

em算法示例<em>标签 (<em> Tag) <em> tag in HTML is used to display the text in emphasized form. <em> tag add semantic meaning to the text, text inside it is treated as emphasized text. HTML中的<em>标记用于以强调形式显示文本。 &…

联合使用 HTML 5、地理定位 API

查找并跟踪位置坐标以用在各种 Web 服务中 在这个由五个部分所组成的系列的第一部分中&#xff0c;您将接触到移动 Web 应用程序中最流行的新技术&#xff1a;地理定位。高端智能手机都内置 GPS&#xff0c;现在您将了解 Web 服务如何使用它。在本文中&#xff0c;您将学到如何…

list下界_下界理论

list下界下界理论 (Lower Bound Theory) Lower bound (L(n)) is a property of the specific problem i.e. the sorting problem, matrix multiplication not of any particular algorithm solving that problem. 下界(L(n))是特定问题(即排序问题)的矩阵&#xff0c;不是解决该…

Mac OSX 安装nvm(node.js版本管理器)

我的系统 1.打开github官网https://github.com/&#xff0c;输入nvm搜索,选择creationix&#xff0f;nvm&#xff0c;打开 2.找到Install script&#xff0c;复制 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash . 3. 打开终端&#xf…

HTML中的类属性

The class attribute in HTML is used to specify or set a single or multiple class names to an element for an HTML and XHTML elements. Its majorly used to indicate a class in a style sheet. HTML中的class属性用于为HTML和XHTML元素指定或设置一个元素名称或多个类…

关于HTML5标签不兼容(IE6~8)

HTML5的语义化标签以及属性&#xff0c;可以让开发者非常方便地实现清晰的web页面布局&#xff0c;加上CSS3的效果渲染&#xff0c;快速建立丰富灵活的web页面显得非常简单。 比较常用的HTML5的新标签元素有&#xff1a; <header>定义页面或区段的头部&#xff1b;<na…

zzz,zzz,zz9_ZZZ的完整形式是什么?

zzz,zzz,zz9ZZZ&#xff1a;睡觉&#xff0c;无聊&#xff0c;累 (ZZZ: Sleeping, Bored, Tired) ZZZ is an abbreviation of "Sleeping, Bored, Tired". ZZZ是“睡觉&#xff0c;无聊&#xff0c;累了”的缩写 。 It is an expression, which is commonly used in …

uboot启动 及命令分析(3)

u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char *name; /* Command Name */ int maxargs; /* maximum number of arguments*/ int repeatable;/* autorepeat allowed? */ /* Implem…

Mozilla开源了VR框架A-Frame

Mozilla创建并开源了A-Frame&#xff0c;这是一个用于在桌面浏览器、智能手机和Oculus Rift上创建VR场景的框架。\\A-Frame是一个在浏览器中创建VR体验的开源框架。该框架由Mozilla的MozVR团队创建和开发。A-Frame使用了一个在游戏开发中经常使用的“实体-组件&#xff08;Enti…

css网格_CSS网格容器

css网格CSS | 网格容器 (CSS | Grid Containers) There are numerous ways to display our list items or elements. For instance, we can display them in the navigation bar, in a menu bar and whatnot. Well, it would be right to say that there are many more such me…

c ++向量库_在C ++中对2D向量进行排序

c 向量库As per as a 2D vector is concerned its a vector of a 1D vector. But what we do in sorting a 1D vector like, 就2D向量而言&#xff0c;它是1D向量的向量 。 但是我们在对一维向量进行排序时所做的工作 sort(vector.begin(),vector.end());We cant do the same …

监听文本框数据修改,特别是微信等客户端直接选择粘贴修改

2019独角兽企业重金招聘Python工程师标准>>> // 手机号码信息加载验证 $(input).bind(input propertychange, function() { initPage.checkName(); }); 转载于:https://my.oschina.net/u/1579617/blog/550488

SSIA的完整形式是什么?

SSIA&#xff1a;主题说明一切 (SSIA: Subject Says It All) SSIA is an abbreviation of "Subject Says It All". SSIA是“主题说明一切”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is written in the subject of the mail…

服务器控件转换成HTML

服务器控件转换成HTML<asp:Label ID"Label1" runat"server" Text"I am label"><asp:Literal ID"Literal1" runat"server" Text"I am a literal"><asp:Panel ID"Panel1" runat"serv…

Eratosthenes筛

什么是Eratosthenes筛&#xff1f; (What is Sieve of Eratosthenes?) Sieve of Eratosthenes is an ancient algorithm of finding prime numbers for any given range. Its actually about maintaining a Boolean table to check for corresponding prime no. Eratosthenes的…

微信iOS多设备多字体适配方案总结

一、背景 2014下半年&#xff0c;微信iOS版先后适配iPad, iPhone6/6plus。随着这些大屏设备的登场&#xff0c;部分用户觉得微信的字体太小&#xff0c;但也有很多用户不喜欢太大的字体。为了满足不同用户的需求&#xff0c;我们做了全局字体设置功能&#xff0c;在【设置-通用…

python矩阵中插入矩阵_Python | 矩阵的痕迹

python矩阵中插入矩阵The sum of diagonal elements of a matrix is commonly known as the trace of the matrix. It is mainly used in eigenvalues and other matrix applications. In this article, we are going to find the trace of a matrix using inbuilt function nu…