您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ajax响应json字符串和json数组的方法

2024/3/30 2:57:45发布15次查看
这次给大家带来ajax响应json字符串和json数组的方法,ajax响应json字符串和json数组的注意事项有哪些,下面就是实战案例,一起来看一下。
最近上班太忙,晚上抽空整理一下ajax请求中,后台返回json字符串和json数组的场景,以及前台的处理示例。
直接看代码。
json字符串的后台响应
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);  } }
json数组的后台响应
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);  } }
前台页面
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>json</title> </head> <body>  <br><br>  <input type="button" value="jsonstr" onclick="jsonstr()" />  <br><br>  <table>  <tr>   <td>username</td>   <td><input id="username"></td>  </tr>  <tr>   <td>id</td>   <td><input id="id"></td>  </tr>  </table>  <br><br><br>  <input type="button" value="jsonarr" onclick="jsonarr()" />  <br><br>  <table border="1" bordercolor="red">  <caption>json array</caption>  <thead>   <tr>   <th>username</th>   <th>id</th>   </tr>  </thead>  <tbody id="tb">  </tbody>  </table> </body> <script type="text/javascript">  // json字符串处理方法  function jsonstr() {  var xhr = new xmlhttprequest();  xhr.open(get, jsonstr);  xhr.onreadystatechange = function(data) {   if (xhr.readystate == 4 && xhr.status == 200) {   // 将json字符串转换为json对象   var obj = eval(( + data.target.responsetext + ));   document.getelementbyid(username).value = obj.name;   document.getelementbyid(id).value = obj.id;   }  };  xhr.send(null);  }    // json数组处理方法  function jsonarr() {  var xhr = new xmlhttprequest();  xhr.open(get, jsonarr);  xhr.onreadystatechange = function(data) {   if (xhr.readystate == 4 && xhr.status == 200) {   // 将json字符串转换为json数组   var obj = eval(( + data.target.responsetext + ));      // 创建代码片段,用于存放表格行   var ofragment = document.createdocumentfragment();      // 根据json数组长度,产生行数据   for (var i=0; i 页面效果图
点击 jsonstr 和 jsonarr 按钮后的效果
好了,整理完毕,示例仅供学习。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ajax如何实现城市二级联动
ajax跨域请求jsonp获取json数据步骤详解(附代码)
以上就是ajax响应json字符串和json数组的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product