JSP+jquery使用ajax方式调用json的实现方法
本文实例讲述了JSP+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);" >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");
希望本文所述对大家JSP程序设计有所帮助。
https://www.uoften.com/program/jsp/20180413/47941.html
页:
[1]