jquery的ajax方式调用json简单例子
前台:
<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript">
//test
function test(uid)
{
if(confirm("确定该用户操作"+uid+"吗?"))
{
var date = new Date();
var param = {uid:uid};
jQuery.get("test.jsp?para=" + date, param,
function(data){
if(data != null)
{
var result = data.result;
//alert("data==="+data);
if(result == 1)
{
alert("操作失败");
}
if(result == 0)
{
alert("操作失败。");
//document.location.href = document.location;
//window.location.reload();
}
}
});
}
}
</script>
<a href="javascript:void(0);" onclick="test(111111111111111111);">submit</a>
后台:
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%
response.setContentType("application/x-json;charset=UTF-8");
String uid=request.getParameter("uid");
int ret=0;
String result = "{\"result\":" + ret + "}";
out.print(result);
%>
注意:返回json格式
如{"result":0}
需要设置 :response.setContentType("application/x-json;charset=UTF-8");