亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery調(diào)用AJAX時(shí)Get和post公用的亂碼解決方法實(shí)例說(shuō)明

 更新時(shí)間:2013年06月04日 16:31:42   作者:  
js調(diào)用AJAX時(shí)Get和post的亂碼解決辦法以前有寫過(guò)的但是使用js代碼比較繁瑣,下面與大家分享下使用jQuery該怎么解決,遇到類似情況的朋友可以參考下哈
以前在新浪博客寫過(guò)js調(diào)用AJAX時(shí)Get和post的亂碼解決辦法,但是使用js代碼比較繁瑣,我們?cè)谑褂胊jax進(jìn)行數(shù)據(jù)交互時(shí)可以使用js的一個(gè)成熟框架---jQuery。

一個(gè)網(wǎng)站的設(shè)計(jì),不管是注冊(cè)登錄還是分頁(yè)查找,都需要提交參數(shù)到服務(wù)器以便得到所需的頁(yè)面數(shù)據(jù)。為了減少用戶因刷新頁(yè)面帶來(lái)的煎熬,ajax誕生。但是初學(xué)者進(jìn)行項(xiàng)目開發(fā)時(shí),會(huì)遇到一個(gè)很煩人的問(wèn)題:中文亂碼。

下面我就通過(guò)一個(gè)簡(jiǎn)單的實(shí)例來(lái)告訴大家哪些地方可能會(huì)導(dǎo)致亂碼,我們需要通過(guò)什么方式來(lái)解決。
我們這個(gè)實(shí)例主要實(shí)現(xiàn)用戶注冊(cè)時(shí)用戶名是否正確(已存在),在焦點(diǎn)移開username文本text時(shí),對(duì)username進(jìn)行異步提交并由servlet進(jìn)行提取判斷,并將結(jié)果返回頁(yè)面做出相應(yīng)提示。

第一步,新建一個(gè)web工程(默認(rèn)GBK格式),取名jQuery_Ajax。在其WebRoot目錄下新建js文件包,將jquery-1.4.4.js放于其中。

第二步,在src下創(chuàng)建servlet包,并編寫Vali.java
復(fù)制代碼 代碼如下:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class Vali extends HttpServlet {
@Override
protectedvoid service(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
StringuserName = URLDecoder.decode(request.getParameter("userName"),"utf-8");
System.out.println(userName);
response.setContentType("text/html;charset=utf-8");
PrintWriter pw =response.getWriter();
if(userName.equals("張三")){
pw.println("錯(cuò)誤");
}else{
pw.println("正確");
}
}
}

從可從代碼看出,含有編碼格式的語(yǔ)句便是解決亂碼的辦法之一。
在代碼中注意:
1.URLDecoder.decode(request.getParameter("userName"),"utf-8")——將頁(yè)面?zhèn)鱽?lái)的數(shù)據(jù)進(jìn)行格式轉(zhuǎn)換并提取
2.response.setContentType("text/html;charset=utf-8")——將響應(yīng)返回值進(jìn)行utf-8編碼后返回頁(yè)面
3.特別注意2中的轉(zhuǎn)換需寫在本方法內(nèi)一切的response之前,否則可能失效
4.本servlet對(duì)數(shù)據(jù)的格式編碼只適合Post方法,若提交方式為GET則提取頁(yè)面數(shù)據(jù)的代碼如下:
復(fù)制代碼 代碼如下:

request.setCharacterEncoding("utf-8");
StringuserName = request.getParameter("userName");
userName= new String(userName.getBytes("iso-8859-1"),"utf-8");

第三步,編寫簡(jiǎn)單注冊(cè)頁(yè)面ajax.jsp
復(fù)制代碼 代碼如下:

<%@ page language="java"import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ajax.jsp' starting page</title>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
<scripttype="text/javascript"src="js/jquery-1.4.4.js"></script>
<scripttype="text/javascript">
function vali(){
$.ajax({
type:"POST",
url:"/jQuery_Ajax/Vali",
data:encodeURI(encodeURI("userName="+$(":text").val())),
success:function(data){
$("span").text(data);
}
});
}
</script>
</head>
<body>
用戶名:<inputtype="text" name="userName"onblur="vali();"/><span></span><br/>
密碼:<inputtype="password" name="password" />
</body>
</html>

在代碼中注意:
1.頁(yè)面要設(shè)置為utf-8,且引入jquery-1.4.4.js
2.ajax通過(guò)POST方法傳遞數(shù)據(jù),注意data的設(shè)置。將返回?cái)?shù)據(jù)填入span標(biāo)簽。

如果使用GET方法傳遞頁(yè)面數(shù)據(jù),js代碼如下:
復(fù)制代碼 代碼如下:

function vali(){
$.ajax({
type:"GET",
url:"/jQuery_Ajax/Vali",
data:encodeURI("userName="+$(":text").val()),
success:function(data){
$("span").text(data);
}
});
}

最后一步,在web.xml配置servlet和映射
復(fù)制代碼 代碼如下:

<servlet>
<description>This is the description of my J2EEcomponent</description>
<display-name>This is the display name of my J2EEcomponent</display-name>
<servlet-name>Vali</servlet-name>
<servlet-class>servlet.Vali</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Vali</servlet-name>
<url-pattern>/Vali</url-pattern>
</servlet-mapping>

經(jīng)過(guò)以上代碼的編寫,本注冊(cè)驗(yàn)證的項(xiàng)目已完成,將其部署至tomcat并通過(guò)網(wǎng)頁(yè)訪問(wèn)。

最后總結(jié)大神的jQuery亂碼問(wèn)題解決方法
1. 檢查頁(yè)面編碼,將頁(yè)面編碼設(shè)置為utf8,如下:
<metahttp-equiv="content-type" content="text/html;charset=utf-8">
2. 檢查servlet,在doPost或doGet方法中添加如下代碼:
response.setContentType("text/xml;charset=utf-8");
3. 修改tomcat文件,在TOMCAT_HOME/conf/server.xml文件中增加URIEncoding=”utf8”:
<Connector port="8080"protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"URIEncoding="utf-8"/>
4. 在工程中新增過(guò)濾器,將編碼方式設(shè)置為utf8
經(jīng)過(guò)以上四步操作后,問(wèn)題依舊。
5. 檢查ie的http header,查看contentType字段,如下:
contentType:"application/x-www-form-urlencoded"
6.檢查firefox的http header,查看contentType字段,如下:
contentType:"application/x-www-form-urlencoded;charset=UTF-8"
對(duì)比5,6兩步,問(wèn)題出現(xiàn)。
7.修改jQuery-1.x.x.js文件,將
contentType:"application/x-www-form-urlencoded"改為下面的代碼
contentType:"application/x-www-form-urlencoded;charset=UTF-8"

相關(guān)文章

最新評(píng)論