Implementation method of JSP+jquery using ajax to call json

  • 2021-08-28 20:49:26
  • OfStack

This paper describes the implementation method of JSP + jquery using ajax to call json. Share it for your reference, as follows:

Front Desk:


<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript">
 //test
 function test(uid)
 {
  if(confirm(" Determine the user action "+uid+" Is it? "))
  {
  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(" Operation failed ");
    }
    if(result == 0)
    {
    alert(" Operation failed. ");
    //document.location.href = document.location;
    //window.location.reload();
    }
     }
  });
  }
 }
</script>
<a href="javascript:void(0);" onclick="test(111111111111111111);">submit</a>

Backstage:

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);
%>

Note: Returns json format

Such as {"result": 0}

Need to set:

response.setContentType("application/x-json;charset=UTF-8");

I hope this article is helpful to everyone's JSP programming.


Related articles: