本文在《ajax的helloworld(1)》进行修改。使用json返回数据。
第一步:
创建User类:
package com.entity; public class User { private int userId; private String userName; private String password; public User(){ } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } }
第二步:
修改RegisterSer:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); User user = new User(); user.setUserName("I am userName"); JSONArray json = JSONArray.fromObject(user); response.getWriter().write(json.toString()); }
(PS..加上response.setContentType("text/html;charset=UTF-8");解决中文乱码问题
本文使用json-lib.jar来拼接json字符串。也可以自己手动写json字符串。)
第三步:
修改js:
$(document).ready(function() { $("#userName").change(function(){ $.ajax({ url:'RegisterSer', data:{userName:$("#userName").val()}, type: "POST", datatype: "json", error:function(){ alert("error occured!!!"); }, success:function(data){ var userJson = eval(data); alert(userJson[0].userName); } }) }); });
加上datatype: "json",表示返回数据类型为json。
使用eval()方法将json字符串转为json对象。
相关推荐
Ajax的HelloWorld1
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/826701
Ajax实现helloworld!实例,eclipse+struts+Ajax初学者必看
关于ajax hello world的代码
ajax例子----Hello world
pear html_ajax实例
JSF2.0实战 - 1-3、Hello World,AJAX体验,onPageLoad示例代码
资源名称:Vaadin简介和HelloWorld 中文WORD版内容简介:本文档主要讲述的是Vaadin简介和HelloWorld;Vaadin(芬兰民间传说中的一种神话生物,是女神也是一种山岭驯鹿的神话祖先) 是一款使用 Apache V2 许可协议的...
第一个Rap程序。 (RAP - Rich Ajax Platform )
基于Spring MVC 2.5 和JQuery 实现的Ajax的一个HelloWorld例子,可以参照这个例子在其他Spring MVC工程加入ajax功能
Struts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例...helloworldStruts 2.1 MVC 学习实例helloworldStruts 2.1 MVC 学习实例helloworld
ajax调用java实例源码Lightstreamer - “Hello World”教程 - HTML 客户端 “Hello World with Lightstreamer”系列的演示是非常基本的示例,我们将交替的字符串“Hello”和“World”,后跟当前时间戳,从服务器推送...
因此,我将在书中讲述ASP.NET AJAX的方方面面,从最基础的“Hello World”示例到复杂的整站程序和底层的实现原理分析,从最简单的客户端效果到其深层次的用户心理学分析及用户体验改善。 正因为本书的介绍范围广泛...
HelloWorld 第一个Ajax应用程序 第2章 浏览器中的JavaScript Test4Navigator.htm User-Agent检测法 Test4Window1.htm window对象属性 test4Alert.htm 系统对话框 Test4Timeout.htm 时间间隔和暂停 Test...
电子书,让大家轻轻松松掌握ajax的开发
仿google、百度的收入提示框,但有所不同,还有待完善。代码简单易懂
本文展示了AngularJS框架实现的hello world代码示例. 如下是一些你在看Hello World 示例和接下来的代码示例时需要重点关注的方面. ng-app, ng-controller, ng-model 指令 带有两个大括弧的模板 步骤 1: 在<Head...
Hello World!分析 完善的AJAX应用程序:AJAX留言板 调试技巧 常见问题
我们经常会使用JavaScript实现动态的改变div里面的内容,尤其是使用ajax的时候,尤为重要。