Home  >  Article  >  Web Front-end  >  Examples of ajax response to json strings and json arrays (graphic tutorial)

Examples of ajax response to json strings and json arrays (graphic tutorial)

亚连
亚连Original
2018-05-22 17:18:101910browse

Below I will bring you an example of ajax responding to json strings and json arrays. Let me share it with you now and give it as a reference for everyone.

I have been too busy at work recently, so I took some time in the evening to sort out the scenarios in which json strings and json arrays are returned in the background during ajax requests, as well as processing examples in the front desk.

Look at the code directly.

Background response of json string

package com.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/jsonStr")
public class JsonStr extends HttpServlet {

 /**
 * 
 */
 private static final long serialVersionUID = 1L;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 // 构造json对象
 String resStr = "{" + "name:" + "\"zhangsan\"," + "id:" + "\"id001\"" + "}";
 
 // 输出json对象到前台
 PrintWriter out = resp.getWriter();
 out.write(resStr);
 out.flush();
 out.close();
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 doGet(req, resp);
 }
}

Background response of json array

package com.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/jsonArr")
public class JsonArr extends HttpServlet {

 /**
 * 
 */
 private static final long serialVersionUID = 1L;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 // 构造json对象
 String resStr1 = "{" + "name:" + "\"zhangsan\"," + "id:" + "\"id001\"" + "}";
 String resStr2 = "{" + "name:" + "\"lisi\"," + "id:" + "\"id002\"" + "}";
 String resStr3 = "{" + "name:" + "\"wangwu\"," + "id:" + "\"id003\"" + "}";
 
 // 构造json数组
 String jsonArr = "[" + resStr1 + "," + resStr2 + "," + resStr3 + "]";
 
 // 输出json数组到前台
 PrintWriter out = resp.getWriter();
 out.write(jsonArr);
 out.flush();
 out.close();
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 doGet(req, resp);
 }
}

Foreground page

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>




Json





username
id





Json Array
Username Id

Page rendering

The effect after clicking the JsonStr and JsonArr buttons

The above is what I compiled for everyone , I hope it will be helpful to everyone in the future.

Related articles:

A brief analysis and solutions to Ajax synchronization and asynchronous issues

Two methods for Ajax to solve redundant refreshes

AjaxSubmit() submits file

The above is the detailed content of Examples of ajax response to json strings and json arrays (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn