jQuery學習筆記(1)--用jQuery實現非同步通訊(用json傳值)具體思路

來源:互聯網
上載者:User

       jQuery是時下比較流行的一個js庫,能夠用簡單的代碼做出理想的效果,就像官網上說的那樣“write less ,do more”。Jquery在一定程度上改寫了以往對JavaScript的寫法,本人就用jquery實現上篇中用ajax實現非同步通訊的效果,感受一下jquery的魅力。

     首先你需要下載jquery的最新的js檔案,並將其引入到檔案中,你也可以在此下載:點我下載。

     這次通訊用的是jquery的jQuery.post(url,[data][callback],[type])方法,這是一個簡單的POST 請求功能以取代複雜 $.ajax 。請求成功時可調用回呼函數。參數為:url,[data],[callback],[type] 相對應的參數類型為String,Map,Function,String:

     ●url:發送請求地址。

     ●data:待發送 Key/value參數。

     ●callback:發送成功時回呼函數。

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

     建立一個jsp檔案jqueryDemo.jsp,代碼如下所示:
複製代碼 代碼如下:
<%@ 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("使用者帳號不可為空!");
$('#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>新使用者註冊</td>
</tr>
<tr>
<td style="width:90px; ">使用者帳號:</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>重複密碼:</td>
<td><input type="password"id="password2" name="password2"></td>
<td></td>
</tr>
<tr>
<td colspan=3 align=center><input type="submit"value="註冊"></td>
</tr>
</table>
</form>
</body>
</html>

     建立一個servlet檔案JqueryServlet.java,代碼如下所示:
複製代碼 代碼如下:
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);
}
}

     好了,現在可以運行了,開啟伺服器,運行此jsp檔案,頁面如下所示:


    當輸入admin時,頁面如下所示:


     當輸入其他的字元時,頁面如下所示:


     可以看出jquery能夠實現ajax的功能,並且代碼更簡潔了。

     只是,最後本人有一個問題遲遲沒有解決,那就是輸入中文時傳到背景值亂碼,按照網上的好多辦法都沒有解決掉,不知道為什麼,誰有更好的方法希望能給我推薦一下,本人不勝感激。

      這是本人學習的結果,允許轉載,歡迎交流,但轉載務必給出本文章的連結地址

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.