zoukankan      html  css  js  c++  java
  • 使用jQuery实现ajax请求

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2021/3/13
      Time: 14:54
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
        <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
        <script type="text/javascript">
          $(function () {
            $("#pBtn").click(function () {
              var pid = $("#pid").val();
              $.ajax(
                      {
                        url:"search",
                        type:"get",
                        data:{
                          "pid":pid
                        },
                        dataType:"json",
                        success:function (resp) {
                          //resp是json对象
                          $("#pname").val(resp.name);
                          $("#pjiancheng").val(resp.jiancheng);
                          $("#pshenghui").val(resp.shenghui);
                        }
                      }
              )
            });
            })
    
        </script>
      </head>
      <body>
        <table border="1">
          <tr>
            <td>城市编号:</td>
            <td><input type="text" id="pid"> <input type="button" id="pBtn" value="搜索"></td>
          </tr>
          <tr>
            <td>城市名称:</td>
            <td><input type="text" id="pname"></td>
          </tr>
          <tr>
            <td>简称:</td>
            <td><input type="text" id="pjiancheng"></td>
          </tr>
          <tr>
            <td>省会:</td>
            <td><input type="text" id="pshenghui"></td>
          </tr>
        </table>
      </body>
    </html>
    

      

    package com.demo.controller;
    
    import com.demo.dao.ProvinceDao;
    import com.demo.entity.Province;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class SearchServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String pid = request.getParameter("pid");
            Province p = null;
    
            if (pid != null  && !"".equals(pid)){
                ProvinceDao dao = new ProvinceDao();
                p = dao.getProvinceById(Integer.valueOf(pid));
            }
    
            ObjectMapper om = new ObjectMapper();
            String jsonStr = om.writeValueAsString(p);
    
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print(jsonStr);
        }
    }
    

      

  • 相关阅读:
    JS倒计时执行操作
    美化radio和checkbox样式
    ajax 多个表单值问题,表单序列化加其它表单值
    .net中的路径问题
    Response.Redirect 打开新窗口的两种方法
    GRIDVIEW FINDCONTROL的使用
    如何验证gridview控件的编辑行?如何获得gridview模板列<ItemTemplate/>中Label值?
    在Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
    在GridView中使用FindControl(2)
    构造函数(C# 编程指南)
  • 原文地址:https://www.cnblogs.com/ethnic/p/14529078.html
Copyright © 2011-2022 走看看