Ajax局部刷新,异步请求

Ajax执行原理:

同步请求和异步请求:

Ajax的语句:

javascript 复制代码
<script src="/js/jquery-1.12.4.min.js"></script>
  <script>
    $(function () {
      $("button").click(function () {
        $.ajax({
          url:"/indexServlet",
          type:"get",//请求方式
          //async:false,//false同步 默认,true异步
          dataType:"text",//返回值类型,常用的json,text,如果要使用result.属性要改为json
          data:{uname:'张三',pwd:1234},//传递数据以键值对的形式
          success:function (result) {//请求后端的结果传给参数result
            $("h1").text(result);
          },
          error:function () {//请求失败
            $("h1").text("请求失败!");
          }
        });
      });
    });
  </script>

在Java中提供的json字符串 ,属性要用""包括。

java 复制代码
@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        //相应编码
        resp.setCharacterEncoding("utf-8");
        //json字符串
        String msg = "登陆成功";
        String s = "{\"code\":200,\"msg\":"+msg+"}";
        PrintWriter out = resp.getWriter();
        out.print(s);
    }
}

阿里巴巴提供的包:

可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。

JSONObject:

JSONObject是一个表示JSON对象的类。它允许你创建、读取和操作JSON对象。

JSON对象由键值对组成,其中键是字符串,值可以是各种类型(如字符串、数字、布尔值、数组、另一个JSON对象等)。

JSONArray:

是一个表示JSON数组的类。它允许你创建、读取和操作JSON数组。

JSON数组是一个有序的值集合,可以包含各种类型的元素(如字符串、数字、布尔值、另一个JSON对象或另一个JSON数组)。

java 复制代码
@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        //相应编码
        resp.setCharacterEncoding("utf-8");
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        if ("张三".equals(uname) && "1234".equals(pwd)){

            jsonObject.put("code",200);
            jsonObject.put("msg","登陆成功");
            //out.print(uname+"登陆成功");
        }else{
            //out.print("登录失败");
            jsonObject.put("code",400);
            jsonObject.put("msg","登陆失败");
        }
        PrintWriter out = resp.getWriter();
        out.print(s);
    }
}
相关推荐
亿元程序员3 分钟前
自从有了 AI,我就再也不想拼 UI 了……
前端
数据狐(Datafox)4 分钟前
淘宝商品详情API实战:多语言代购商城自动同步数据完整方案
开发语言·前端·数据库·爬虫·json
hasty15 分钟前
HTML 已经转义,为何仍有 XSS?Sharp srcdoc 漏洞中的第二次解析
前端·html·xss
涛涛ing17 分钟前
WebMCP正式进入Origin Trial:AI Agent不再“装人”,前端交互范式正在被重写
前端
y = xⁿ22 分钟前
关于Agent智能体开发
开发语言·前端·javascript
梅孔立25 分钟前
Codex 完整教程(Node.js安装 + npm安装Codex + DeepSeek配置 + 快捷键 + 设置)
前端·npm·node.js
事圆则缓33 分钟前
Flutter 状态管理框架对比(一):先看地图,再选工具
前端·javascript·flutter
中杯可乐多加冰35 分钟前
解决方案:CEB 文件 Web 端在线预览方案,Linux部署ceb文件在线预览工具
linux·运维·前端·ceb
晚风醉蝶41 分钟前
webpack 模块提取
前端·webpack·node.js·ast·逆向分析
IMPYLH1 小时前
HTML 的 <tfoot> 元素
前端·javascript·html