欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery學(xué)習(xí)筆記(1)--用jQuery實(shí)現(xiàn)異步通信(用json傳值)具體思路

 更新時(shí)間:2013年04月08日 17:29:36   作者:  
這是一個(gè)簡(jiǎn)單的POST 請(qǐng)求功能以取代復(fù)雜 $.ajax,請(qǐng)求成功時(shí)可調(diào)用回調(diào)函數(shù),感興趣的朋友可以參考下哈,希望對(duì)你有所幫助

       jQuery是時(shí)下比較流行的一個(gè)js庫(kù),能夠用簡(jiǎn)單的代碼做出理想的效果,就像官網(wǎng)上說(shuō)的那樣“write less ,do more”。Jquery在一定程度上改寫(xiě)了以往對(duì)JavaScript的寫(xiě)法,本人就用jquery實(shí)現(xiàn)上篇中用ajax實(shí)現(xiàn)異步通信的效果,感受一下jquery的魅力。

     首先你需要下載jquery的最新的js文件,并將其引入到文件中,你也可以在此下載:點(diǎn)我下載。

     這次通信用的是jquery的jQuery.post(url,[data][callback],[type])方法,這是一個(gè)簡(jiǎn)單的POST 請(qǐng)求功能以取代復(fù)雜 $.ajax 。請(qǐng)求成功時(shí)可調(diào)用回調(diào)函數(shù)。參數(shù)為:url,[data],[callback],[type] 相對(duì)應(yīng)的參數(shù)類(lèi)型為String,Map,Function,String:

     ●url:發(fā)送請(qǐng)求地址。

     ●data:待發(fā)送 Key/value參數(shù)。

     ●callback:發(fā)送成功時(shí)回調(diào)函數(shù)。

     ●type:返回內(nèi)容格式,xml,html, script, json, text, _default)

     新建一個(gè)jsp文件jqueryDemo.jsp,代碼如下所示:

復(fù)制代碼 代碼如下:

<%@ page language="java"contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=GB18030">
<title>jquery</title>
<style type="text/css">
table.demo{border-collapse: collapse;margin-top: 50px;margin-left: 220px;}
table.demo th,td {padding: 0; border: 1px solid #000;}
#img,#msg{position: static;float: left;}
#account,#password1,#password2{margin-left: 10px;}
#img{margin-left: 10px;}
</style>
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function accountCheck(){
var account=$('#account').val();
if(account==""){
alert("用戶帳號(hào)不能為空!");
$('#img').html("");
$('#msg').text("");
return;
}
$.post('JqueryServlet',{strAccount:account},function(data){
eval("data="+data);
if(data.success){
$('#img').html("<img src='img/cross.png'/>");
}else{
$('#img').html("<img src='img/tick.png'/>");
}
$('#msg').text(data.msg);
});
}
</script>
</head>
<body>
<form action=""method="post" >
<table class="demo" style="width: 450px;height: 200px;">
<tr>
<td colspan=3 align=center>新用戶注冊(cè)</td>
</tr>
<tr>
<td style="width:90px; ">用戶帳號(hào):</td>
<td style="width:185px; "><input type="text"id="account" name="account"onblur="accountCheck();"><font color=red>*</font></td>
<td style="width:175px; ">
<div id="img" class="img"></div>
<div id="msg"class="msg"></div>
</td>
</tr>
<tr>
<td>用戶密碼:</td>
<td><input type="password"id="password1" name="password1"></td>
<td></td>
</tr>
<tr>
<td>重復(fù)密碼:</td>
<td><input type="password"id="password2" name="password2"></td>
<td></td>
</tr>
<tr>
<td colspan=3 align=center><input type="submit"value="注冊(cè)"></td>
</tr>
</table>
</form>
</body>
</html>

     新建一個(gè)servlet文件JqueryServlet.java,代碼如下所示:

復(fù)制代碼 代碼如下:

package com.ldfsoft.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*Servlet implementation class JqueryServlet
*/
public class JqueryServlet extendsHttpServlet {
privatestatic final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JqueryServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequestrequest, HttpServletResponse response)
*/
protectedvoid service(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String account=request.getParameter("strAccount");
PrintWriter out=response.getWriter();
String str=""; //用以json傳值
if(account.equals("admin")){
str="{success:true,msg:'該賬戶已存在'}";
}else{
str="{success:false,msg:'該賬戶可以使用'}";
}
out.write(str);
}
}

     好了,現(xiàn)在可以運(yùn)行了,打開(kāi)服務(wù)器,運(yùn)行此jsp文件,頁(yè)面如下所示:


    當(dāng)輸入admin時(shí),頁(yè)面如下所示:


     當(dāng)輸入其他的字符時(shí),頁(yè)面如下所示:


     可以看出jquery能夠?qū)崿F(xiàn)ajax的功能,并且代碼更簡(jiǎn)潔了。

     只是,最后本人有一個(gè)問(wèn)題遲遲沒(méi)有解決,那就是輸入中文時(shí)傳到后臺(tái)的值亂碼,按照網(wǎng)上的好多辦法都沒(méi)有解決掉,不知道為什么,誰(shuí)有更好的方法希望能給我推薦一下,本人不勝感激。

      這是本人學(xué)習(xí)的結(jié)果,允許轉(zhuǎn)載,歡迎交流,但轉(zhuǎn)載務(wù)必給出本文章的鏈接地址

相關(guān)文章

最新評(píng)論