发布于2020-11-19 20:48 阅读(905) 评论(0) 点赞(3) 收藏(3)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function ajaxRequest() { // 1、我们首先要创建XMLHttpRequest var xmlHttpRequest = new XMLHttpRequest(); // 2、调用open方法设置请求参数 xmlHttpRequest.open("GET", "http://localhost:8080/16_json_ajax_i18n/ajaxServlet?action=javaScriptAjax", true); // 4、在send方法前绑定onreadystatechange事件,处理请求完成后的操作。 xmlHttpRequest.onreadystatechange = function(){ if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ var jsonObj = JSON.parse(xmlHttpRequest.responseText); //把响应的数据显示在页面上 document.getElementById("div01").innerHTML = "编号:" + jsonObj.id + ", 姓名:" + jsonObj.name; } }; // 3、调用send方法发送请求 xmlHttpRequest.send(); } </script> </head> <body> <button onclick="ajaxRequest()">ajax request</button> <div id="div01"> </div> </body> </html>
创建AjaxServlet程序接收Ajax请求并响应数据:
package com.zixue.servlet; import com.google.gson.Gson; import com.zixue.bean.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author Mr Guo * @create 2020-11-18 10:48 */ public class AjaxServlet extends BaseServlet { protected void javaScriptAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Ajax请求过来了。"); Person person = new Person(1, "郭鹏"); //json格式的字符串 Gson gson = new Gson(); String personJsonString = gson.toJson(person); resp.getWriter().write(personJsonString); } }
访问效果:
示例:
// ajax请求 $("#ajaxBtn").click(function(){ $.ajax({ url: "http://localhost:8080/16_json_ajax_i18n/ajaxServlet", type: "GET", // data: "action=jQueryAjax", data: {action:"jQueryAjax"}, success: function (data) { //如果dataType的类型为text var jsonObj = JSON.parse(data); $("#div888").html("编号:" + jsonObj.id + ", 姓名:" + jsonObj.name); //如果dataType的类型为json // $("#div888").html("编号:" + data.id + ", 姓名:" + data.name); }, dataType: "text" }); });
请求的Servlet程序:
protected void jQueryAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("jQueryAjax请求过来了。"); Person person = new Person(1, "郭鹏"); //json格式的字符串 Gson gson = new Gson(); String personJsonString = gson.toJson(person); resp.getWriter().write(personJsonString); }
效果:
// ajax--get请求 $("#getBtn").click(function(){ $.get("http://localhost:8080/16_json_ajax_i18n/ajaxServlet", "action=jQueryGet", function (data) { $("#div888").html("get 编号:" + data.id + ", 姓名:" + data.name); }, "json"); }); // ajax--post请求 $("#postBtn").click(function(){ // post请求 $.get("http://localhost:8080/16_json_ajax_i18n/ajaxServlet", "action=jQueryPost", function (data) { $("#div888").html("Post 编号:" + data.id + ", 姓名:" + data.name); }, "json"); });
// ajax--getJson请求 $("#getJSONBtn").click(function(){ // 调用 $.getJSON("http://localhost:8080/16_json_ajax_i18n/ajaxServlet", "action=jQueryGetJSON", function (data) { $("#div888").html("getJSON 编号:" + data.id + ", 姓名:" + data.name); }); });
ajax请求:
// ajax请求 $("#submit").click(function(){ // 调用 $.getJSON("http://localhost:8080/16_json_ajax_i18n/ajaxServlet", "action=jQuerySerialize&" + $("#form01").serialize(), function (data) { $("#div888").html("Serialize 编号:" + data.id + ", 姓名:" + data.name); }); });
Servlet程序:
protected void jQuerySerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("jQuerySerialize 请求过来了。"); System.out.println(req.getParameter("username")); System.out.println(req.getParameter("password")); Person person = new Person(1, "郭鹏"); //json格式的字符串 Gson gson = new Gson(); String personJsonString = gson.toJson(person); resp.getWriter().write(personJsonString); }
访问:
控制台:
原文链接:https://www.cnblogs.com/897463196-a/p/13995320.html
作者:java大师
链接:http://www.javaheidong.com/blog/article/1142/455a4e28d3ca935fb503/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!