Jquery Ajax 练习

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>jquery-Test1</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<script src="jquery/jquery-1.11.1.js"></script>
<script src="jquery/jquery-1.11.1.min.js"></script> 	
</head>
<script>
	$(document).ready(function() {
		$("#s").click(function() {
			$.ajax({
				url : 'jqueryTest/jqueryTestJosn.action',
				type : 'post',
				data:{username:username,password:password},   
				//data : dataString,
				async : true, //默认为true 异步     
				error : function() {
					alert('error');
				},
				success : function(data) {
					//list 类型  json  var s = "[\"a\", \"b\"]";
					alert('success'+data);
					//var jsonData = eval(data);
					var jsonData = eval("(" + data + ")");//为Map 是要这个方法不会报错
					alert('success2'+data);
					$.each(jsonData, function(key, val) {
						alert('_mozi数组中 ,索引 : ' + key + ' 对应的值为: ' + val);
					});
				}
			});
		});
		
		$("#b").click(function(){
			$.getJSON("jqueryTest/jqueryTestJosn.action", { username: "John", password: "2pm" }, function(json){
					var jsonData = eval("(" + json + ")");//为Map 是要这个方法不会报错
					alert('success2'+json);
					$.each(jsonData, function(key, val) {
						alert('_mozi数组中 ,索引 : ' + key + ' 对应的值为: ' + val);
					});
				});
		});
	});
</script>
<style>

</style>

<body>
	<button type="button" id="s">Ajax Json Test</button>
	<button type="button" id="b">Ajax #getJson</button>
</body>

 

package com.sf.test.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.sf.test.bean.Test1;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonTest1Action extends ActionSupport{
	
	private String data;
	private String username;
	private String password;
	
	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 String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String arrayToJson(){
		boolean[] boolArray = new boolean[]{true,false,true};
		
		JSONArray jsonArray = JSONArray.fromObject(boolArray);
		System.out.println(jsonArray);
		data = jsonArray.toString();
		//[true,false,true]
		return Action.SUCCESS;
	}
	
	public String listToJson(){
		List list = new ArrayList();
		list.add("first");  
	    list.add("second");  
	      
	    JSONArray jsonArray = JSONArray.fromObject(list); 
	    data = jsonArray.toString();
	    System.out.println(data);
	    //["first","second"]
	    return Action.SUCCESS;
	}
	
	public String mapToJson(){
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("1", "小王");
			map.put("2", new Integer(1));
			map.put("3", Boolean.TRUE);
			
			JSONObject  mapJson = JSONObject .fromObject(map);
			this.data = mapJson.toString();
			System.out.println(data);
			//{"3":true,"2":1,"1":"小王"}
			return Action.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}
	
	public String createToJson(){
		JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");  
		this.data = jsonArray.toString();
        System.out.println(jsonArray);  
		return Action.SUCCESS;
	}
	
	public String beanToJson(){
		Test1 test = new Test1();
		test.setC1("A");
		test.setC2("B");
		test.setC3("C");
		
		System.out.println("UserName:"+username);
		System.out.println("PassWord::"+password);
		JSONObject jsonBean = JSONObject.fromObject(test);  
		this.data = jsonBean.toString();
		System.out.println(jsonBean); 
		//{"c1":"A","c2":"B","c3":"C"}
		return Action.SUCCESS;
	}
	
/*	   public void json2bean() {  
	        String json = "{name=/"json2/",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";  
	        JSONObject jb = JSONObject.fromString(json);  
	        JSONObject.toBean(jb, Test1.class);  
	        System.out.println();  
	    } */
	
	// json  var s = "[\"a\", \"b\"]";
	
/*	Json必需的包
	commons-httpclient-3.1.jar
	commons-lang-2.4.jar
	commons-logging-1.1.1.jar
	json-lib-2.2.3-jdk13.jar
	ezmorph-1.0.6.jar
	commons-collections-3.2.jar*/
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值