ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
更新時(shí)間:2018年08月14日 09:49:06 作者:MQ-HZ
今天小編就為大家分享一篇ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
效果圖

jsp代碼
<form > 姓名:<input name="name" type="text"/> 年齡:<input name="age" type="text"/> <input type="button" class="get" value="get提交"/> <input type="button" class="post" value="post提交"/> <input type="button" class="ajax" value="ajax提交"/> </form> <div id="box"></div>
servlet代碼
//get
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String age = request.getParameter("age");
if (name == null || name == "") {
name = "測(cè)試名字admin";
}
if (age == null || age == "") {
age = "測(cè)試年齡100";
}
user user = new user(1, name, age);
PrintWriter out = response.getWriter();
JSONObject jsonObj = JSONObject.fromObject(user);
out.print(jsonObj);
out.flush();
out.close();
}
//post
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
if (name == null || name == "") {
name = "測(cè)試名字admin";
}
String age = request.getParameter("age");
if (age == null || age == "") {
age = "測(cè)試年齡100";
}
user user = new user(1, name, age);
PrintWriter out = response.getWriter();
JSONObject jsonObj = JSONObject.fromObject(user);
out.print(jsonObj);
out.flush();
out.close();
}
JS核心代碼
<script type="text/javascript">
//get
$(document).ready(function() {
$('form .get').click(function() {
$.get('ajaxServlet',function(response,status,xhr){
var dataObj = eval("(" + response + ")");
$("#box").html(response);
alert(dataObj.name);
});
});
//post
$('form .post').click(function() {
$.post('ajaxServlet',function(response,status,xhr){
var dataObj = eval("(" + response + ")");
$("#box").html(response);
});
});
//ajax
$('form .ajax').click(function() {
alert($("[name='name']").val());
$.ajax({
type:'get',
url:'ajaxServlet',
data:{
name:$("[name='name']").val(),
age:$("[name='age']").val()
},
success:function(response, status, xhr){
$("#box").html(response);}
});
});
});
</script>
以上這篇ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)history解決ajax不支持前進(jìn)/后退/刷新的問(wèn)題
下面小編就為大家?guī)?lái)一篇通過(guò)history解決ajax不支持前進(jìn)/后退/刷新的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)
這篇文章主要介紹了laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
一個(gè)簡(jiǎn)單Ajax類庫(kù)及使用方法實(shí)例分析
這篇文章主要介紹了一個(gè)簡(jiǎn)單Ajax類庫(kù)及使用方法,結(jié)合實(shí)例形式分析了ajax類庫(kù)的源碼與具體使用技巧,需要的朋友可以參考下2016-04-04
用 ajax 的方法解決網(wǎng)頁(yè)廣告顯示的問(wèn)題
用 ajax 的方法解決網(wǎng)頁(yè)廣告顯示的問(wèn)題...2006-12-12
Ajax post請(qǐng)求跳轉(zhuǎn)頁(yè)面
這篇文章主要介紹了Ajax post請(qǐng)求跳轉(zhuǎn)頁(yè)面的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
使用Ajax或Easyui等框架時(shí)的Json-lib的處理方案
這篇文章主要介紹了使用ajax或easyui等框架時(shí)的Json-lib的處理方案 ,需要的朋友可以參考下2017-06-06

