今天又学到了一点新知识,脑子记不住东西特把它记录下来!
页面ajax请求后台时一般都是返回字符串进行判断,要是返回list或者对象时该怎么办?
第一种:ajax接收到list并返回给前台
js代码:
- function test(obj){
- var str = $("#tt").val();
- webTest(str,function(result){
- alert(result);
- var list = eval(result);
- 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);
}
});
}</pre><span style="white-space:pre;"></span>上面红字webTest方法(这里是调用webservice,和ajax处理流程一样)<div class="dp-highlighter bg_javascript"><div class="bar"><div class="tools"><b>[javascript]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 540px; top: 803px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent"></div><div style="position: absolute; left: 540px; top: 803px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=16&height=16" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-c"><li class="alt"><span><span class="keyword">var</span><span> webTest = </span><span class="keyword">function</span><span> (test, callback) { </span></span></li><li class=""><span> <span class="keyword">var</span><span> soapMessage = </span><span class="string">'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bs="http://inter.hs.com/">'</span><span> </span></span></li><li class="alt"><span> + <span class="string">'<soapenv:Header/>'</span><span> </span></span></li><li class=""><span> + <span class="string">'<soapenv:Body>'</span><span> </span></span></li><li class="alt"><span> + <span class="string">'<bs:test>'</span><span> </span></span></li><li class=""><span> + <span class="string">'<para>'</span><span> + test + </span><span class="string">'</para>'</span><span> </span></span></li><li class="alt"><span> + <span class="string">'</bs:test>'</span><span> </span></span></li><li class=""><span> + <span class="string">'</soapenv:Body>'</span><span> </span></span></li><li class="alt"><span> + <span class="string">'</soapenv:Envelope>'</span><span>; </span></span></li><li class=""><span> PostData(soapMessage, callback); </span></li><li class="alt"><span>} </span></li></ol></div><pre class="javascript" name="code" style="display: none;">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){
- if(result == "false"){
- alert("用户名或者密码错误");
- }else{
- var userinfo = eval(result)[0];
- 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>
-
-
-
-
-
-
-
- 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;
}
如有不对,还请各位大牛指正!