1,jsp

<title>測試Ajax</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript">
$(function(){
$("#xxx").blur(function(){
var value = $('#xxx').val();
$.ajax({
url:"/java_web02/AjaxServlet", //要請求服務(wù)器的url
data:{val:value}, //這是一個對象,表示請求參數(shù)val=xx, 服務(wù)器端可以使用request.getParameter()來獲取
async:true, //是否為異步請求
cache:false, //是否緩存結(jié)果
type:"POST", //請求方式
dataType:"json", //服務(wù)器返回的數(shù)據(jù)是什么類型
success:function(result){//這個函數(shù)在服務(wù)器執(zhí)行成功時會被調(diào)用,參數(shù)result就是服務(wù)器返回的值
$('label').text(result.name + "," + result.age);
}
});
});
});
</script>
</head>
<body>
用戶名:<input type="text" name="name" id="xxx"/><label></label><br />
年齡:<input type="text" name="age" />
</body>2,servlet
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置編碼格式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//獲取前臺傳來的數(shù)據(jù)
String value = request.getParameter("val");
if(value.equals("xiaoxiao")){
String str = "{\"name\":\"好人\", \"age\":23}"; //用\來轉(zhuǎn)義
response.getWriter().print(str);
}else {
String str = "{\"name\":\"壞人\", \"age\":89}";
response.getWriter().print(str);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁題目:Ajax提交數(shù)據(jù)-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.chinadenli.net/article38/djdhpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、做網(wǎng)站、網(wǎng)站設(shè)計公司、Google、網(wǎng)站建設(shè)、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容