DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> ajax的兩種提交方式(get/post)和兩種版本
ajax的兩種提交方式(get/post)和兩種版本
編輯:AJAX基礎知識     
最近比較閒,就把以前用過的技術串一下做個手札,方便以後自己偷懶,小鳥你們幸福了。

首先主要是將javascript版本ajax做下注釋:ajax異步刷新主要是將所需條件拼成字符串傳入後台,處理之後,直接調用回調函數將所得數據返還給頁面,並加以顯示,因為還在本頁面,所以不用刷新頁面,懂了了吧,本篇也用encodeURI對字符串做了加密,並在類裡做了解碼,其中需要一些注意的地方在源碼裡做了注釋。get/post兩種提交方式,但get提交容易亂碼,一定多加注意

jsp頁面:
復制代碼 代碼如下:
<%@ 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//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" >
var xmlHttp;
function createxmlHttpRequest(){
if(window.XMLHttpRequest){
xmlHttp= new XMLHttpRequest();//IE7+,FireFox,Opera,Safari,Chrome
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function test(){//get
//獲取參數
//var unames=encodeURI(document.getElementById("username").value);//一次編碼java用new String(name.getBytes("ISO8859-1"), "UTF-8")解碼
var unames=encodeURI(encodeURI(document.getElementById("username").value));//兩次編碼才能用java.net.URLDecoder.decode(name,"utf-8");解碼
var pws=encodeURI(document.getElementById("password").value);
createxmlHttpRequest();
xmlHttp.onreadystatechange=readyState;
//function(){
//alert(xmlHttp.readyState+"=="+xmlHttp.status);//判斷請求狀態
//}
xmlHttp.open("get","AjaxServlet1?msg=gets&name="+unames+"&pwd="+pws+"&timeStamp="+new Date().getTime(),true); //get 方式提交中文會出現亂碼,encodeURI()/encodeURIComponent()將中文轉成16進制編碼,把字符串作為URI進行編碼
xmlHttp.send(null);
}
function testp(){//post
//獲取參數
var unames=document.getElementById("username").value;
var pws=document.getElementById("password").value;
createxmlHttpRequest();
xmlHttp.onreadystatechange=readyState;
xmlHttp.open("post","AjaxServlet1",true);
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var str="msg=posts&name="+unames+"&pwd="+pws+"&timeStamp="+new Date().getTime();
xmlHttp.send(str);//send 可用於傳參
}

function readyState(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var msg= xmlHttp.responseText;
//alert(msg);
document.getElementById("result").innerHTML=msg;
}
}
}
</script>
<title>js異步刷新</title>
</head>

<body>
<center>
<div id="response">
</div>
用戶:<input type="text" name="uname" id="username"><br>
密碼:<input type="text" name="pw" id="password"><br>
<input type="button" name="button" value="get確定" onclick="test();"/>
<input type="button" name="button" value="post確定" onclick="testp();">
<div id="result">
</div>
</center>
</body>

</html>

這裡是servlet/action Java代碼:
復制代碼 代碼如下:
package com.cstp.javascript;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class AjaxServlet1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//設置編碼,防止亂碼
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//接收參數
String msg=request.getParameter("msg");
if(msg.equals("gets")){
// String name=new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8"); //一次編碼,java裡進行解碼操作
String name=java.net.URLDecoder.decode(request.getParameter("name"),"utf-8"); //decode解碼頁面必須兩次編碼,java裡進行解碼操作
String pwd=request.getParameter("pwd");
System.out.println(name+","+pwd);
PrintWriter out = response.getWriter();
out.println("ajax響應get,結果返回"+name+","+pwd);
}else if(msg.equals("posts")){
String name=new String(request.getParameter("name").getBytes("utf-8"), "UTF-8"); //一次編碼,java裡進行解碼操作
String pwd=request.getParameter("pwd");
System.out.println(name+","+pwd);
PrintWriter out = response.getWriter();
out.println("ajax響應post,結果返回"+name+","+pwd);
}

}
}

上面是javascript版ajax,下面將喜歡jquery版的也分享給JQ友們:

頁面上:
復制代碼 代碼如下:
<script type="text/javascript">
//方式①
function circum(lon,lat){
$.ajax({
url: "JQAjaxServlet?method=jsons",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
type : 'post',
dataType:"json",
async: false,
data : { //傳參給後台
'lon' : lon,
'lat' : lat
},
success: function (data) { // 接後台返回result
在這裡data為後台返回數據,你可以盡情處理了
}
});
}

</script>

後台:servlet/action裡

類裡對數據處理的方法同上,就不再累贅了
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved