今天又学到了一点新知识,脑子记不住东西特把它记录下来!
页面ajax请求后台时一般都是返回字符串进行判断,要是返回list或者对象时该怎么办?
第一种:ajax接收到list并返回给前台
js代码:
- function test(obj){
- var str = $("#tt").val();
- webTest(str,function(result){
- alert(result);//将后台返回结果alert一下
- var list = eval(result);//解析json
- for(var i = 0;i < list.length;i++){//循环遍历数据
- var userinfo = list[i];
- alert(userinfo.name+"+"+userinfo.age+"+"+userinfo.sex+"+"+userinfo.address);
- }
- });
- }
function test(obj){
var str = $("#tt").val();
webTest(str,function(result){
alert(result);//将后台返回结果alert一下
var list = eval(result);//解析json
for(var i = 0;i < list.length;i++){//循环遍历数据
var userinfo = list[i];
alert(userinfo.name+"+"+userinfo.age+"+"+userinfo.sex+"+"+userinfo.address);
}
});
}
上面红字webTest方法(这里是调用webservice,和ajax处理流程一样)
- var webTest = function (test, callback) {
- var soapMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bs="http://inter.hs.com/">'
- + '<soapenv:Header/>'
- + '<soapenv:Body>'
- + '<bs:test>'
- + '<para>' + test + '</para>'
- + '</bs:test>'
- + '</soapenv:Body>'
- + '</soapenv:Envelope>';
- PostData(soapMessage, callback);
- }
var webTest = function (test, callback) {
var soapMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bs="http://inter.hs.com/">'
+ '<soapenv:Header/>'
+ '<soapenv:Body>'
+ '<bs:test>'
+ '<para>' + test + '</para>'
+ '</bs:test>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
PostData(soapMessage, callback);
}
后台代码:
- @Override
- public String test(String str) {
- List<UserInfo> list = new ArrayList<UserInfo>();
- UserInfo ui = new UserInfo();
- ui.setName("tom");
- ui.setAge(22);
- ui.setSex("男");
- ui.setAddress("陕西西安");
- list.add(ui);
- JSONObject ja = JSONObject.fromObject(list);//将list包装成json传递给前台
- return ja.toString();
- }
@Override
public String test(String str) {
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo ui = new UserInfo();
ui.setName("tom");
ui.setAge(22);
ui.setSex("男");
ui.setAddress("陕西西安");
list.add(ui);
JSONObject ja = JSONObject.fromObject(list);//将list包装成json传递给前台
return ja.toString();
}
运行结果:
第二种情况,接收对象数据返回:
js代码:
- <script>
- $("div.submit_op>input").click(function(){
- var uname = $("#acount").val();
- var pass = $("#pass").val();
- if(uname==""){
- alert("请输入用户名");
- }else if(pass == ""){
- alert("请输入密码");
- }else{
- ecLogin(uname,pass,function(result){//这一步可以堪称是ajax请求后台,result是返回结果
- if(result == "false"){
- alert("用户名或者密码错误");
- }else{
- var userinfo = eval(result)[0];//当是单个对象时,虽不用循环遍历,但是需要【0】取</span>
- alert(userinfo.user_reg_id);
- }
- });
- }
- })
- </script>
<script>
$("div.submit_op>input").click(function(){
var uname = $("#acount").val();
var pass = $("#pass").val();
if(uname==""){
alert("请输入用户名");
}else if(pass == ""){
alert("请输入密码");
}else{
ecLogin(uname,pass,function(result){//这一步可以堪称是ajax请求后台,result是返回结果
if(result == "false"){
alert("用户名或者密码错误");
}else{
var userinfo = eval(result)[0];//当是单个对象时,虽不用循环遍历,但是需要【0】取</span>
alert(userinfo.user_reg_id);
}
});
}
})
</script>
- /**
- * 登录接口
- * @param phone 手机号
- * @param pass 密码
- * @param callback func 回调函数,原型function(string) 其中参数为登录之后的用户对象或者null
- * @returns
- */
- var ecLogin = function (phone, pass, callback) {
- alert(phone);
- var soapMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bs="http://inter.hs.com/">'
- + '<soapenv:Header/>'
- + '<soapenv:Body>'
- + '<bs:login>'
- + '<phone>' + phone + '</phone>'
- + '<pass>' + pass + '</pass>'
- + '</bs:login>'
- + '</soapenv:Body>'
- + '</soapenv:Envelope>';
- PostData(soapMessage, callback);
- }
/**
* 登录接口
* @param phone 手机号
* @param pass 密码
* @param callback func 回调函数,原型function(string) 其中参数为登录之后的用户对象或者null
* @returns
*/
var ecLogin = function (phone, pass, callback) {
alert(phone);
var soapMessage = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bs="http://inter.hs.com/">'
+ '<soapenv:Header/>'
+ '<soapenv:Body>'
+ '<bs:login>'
+ '<phone>' + phone + '</phone>'
+ '<pass>' + pass + '</pass>'
+ '</bs:login>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
PostData(soapMessage, callback);
}
后台代码:
- @Override
- public String login(String strPhone, String password) {
- String str = "false";
- Connection conn = DBManager.getConnection();
- String sql = "select * from tb_user_login where user_phone = ? and user_pwd = ?";
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- ps = conn.prepareStatement(sql);
- ps.setString(1, strPhone);
- ps.setString(2, password);
- rs = ps.executeQuery();
- while (rs.next()){
- UserLogin ul = new UserLogin();
- ul.setUser_reg_id(rs.getString(1));
- ul.setUser_phone(rs.getString(2));
- JSONObject ja = JSONObject.fromObject(ul);
- str = ja.toString();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return str;
- }
@Override
public String login(String strPhone, String password) {
String str = "false";
Connection conn = DBManager.getConnection();
String sql = "select * from tb_user_login where user_phone = ? and user_pwd = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, strPhone);
ps.setString(2, password);
rs = ps.executeQuery();
while (rs.next()){
UserLogin ul = new UserLogin();
ul.setUser_reg_id(rs.getString(1));
ul.setUser_phone(rs.getString(2));
JSONObject ja = JSONObject.fromObject(ul);
str = ja.toString();
}
} catch (SQLException e) {
e.printStackTrace();
}
return str;
}
如有不对,还请各位大牛指正!