Prototype 1.6.1和jquery 1.7.2的Ajax Post请求方法对比

本文通过具体的示例代码展示了如何使用Prototype 1.6.1及jQuery 1.7.2实现AJAX请求,包括POST请求的发送、服务器响应处理、JSON数据解析等关键步骤,并介绍了服务器端Servlet如何响应并返回JSON格式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//使用Prototype 1.6.1的ajax请求方法
function prototypeRequest() {
	new Ajax.Request('servlet/SendInjunction', {
				method : 'post',
				parameters : {
					company : 'example',
					type : 'onePosition'
				},
				onSuccess : function(response) {
					if (200 == response.status) {
						alert("Call is success");
						var content = response.responseText;
						var result = response.responseText.evalJSON();
						alert('content:' + content);
						alert('result.code:' + result.code);
					}
				},
				onFailure : function failureFunc(response) {
					alert("Call is failed");
				}
			});
}
/*
 * function successFunc(response) { if (200 == response.status) { alert("Call is
 * success"); var content = response.responseText; alert('content:'+content); } }
 * 
 * function failureFunc(response) { alert("Call is failed"); }
 */


//jquery 1.7.2的ajax请求方法
function jqueryRequest() {
	$.post('servlet/SendInjunction', {
				company : 'example',
				type : 'onePosition'
			}, function(data) {
				alert('data:' + data);
				alert(data.code);
			}, "json");
}


Servlet部分:

		String company = request.getParameter("company");
		System.out.println("companyP:"+company);
		HashMap hm=new HashMap();
		hm.put("companyP",company);
		hm.put("code",10000);
		JsonUtil.responseJsonObject(response, hm);


responseJsonObject:

	public static void responseJsonObject(HttpServletResponse response, Object obj) throws IOException{ 
		String json;
		PrintWriter out = null;
		try {
			json = JSONUtil.serialize(obj);
			//System.out.println("json:" + json);
			out = response.getWriter();
			out.print(json);
			out.flush();		
		} catch (JSONException e) {
			e.printStackTrace();
		}finally{
			if(out != null){
				out.close();
			}
		}
	}


用到了googlecode中的jsonplugin.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷月宫主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值