zoukankan      html  css  js  c++  java
  • Spring MVC 获取不到 ajaxs 参数值

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <script type="text/javaScript" src="${pageContext.request.contextPath }/js/jquery-3.6.0.min.js">
    </script>
    <body>
    
    <form name="Form2"  id="form2" action ="" method="post"  enctype="multipart/form-data"> 
     姓名:<input type="text" name="name" id="name">
     密码:<input type="text" name="password" id="password">
     <input type="button" value="测试" onclick="testJson()" />
    </form> 
    
    </body>
    <script type="text/javascript">
    function testJson() {
        alert("进来了");
        var name = $("#name").val();
        var password = $("#password").val();
        alert("姓名"+name+",学校:"+password);
        $.ajax({
            //请求路径
            url : "fileupload",
            //请求类型
            type : "post",
            //data表示发送的数据
            data :{
                    "name" : name,
                    "password" : password
                }, //定义发送请求的数据格式为JSON字符串
            contentType : "application/json;charset=utf-8",
            //定义回调响应的数据格式为JSON字符串,该属性可以省略
            dataType : "json",
            //成功响应的结果
            success : function(data) {
                alert(data);
                if (data != null) {
                    alert("输入的用户名:" + data.name + ",密码:" + data.password);
                }
            }
        });
    }  
     
    
    </script>
    
    </html>

    上面是jsp页面

    controller方法

    package com.zjf.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
    
    @Controller
    public class FileUpLoad {
    
        @RequestMapping("/fileupload")
        public ModelAndView fileupload(HttpServletRequest request,HttpServletResponse rs) {
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            System.out.println("name:"+name+",password:"+password);
            Map<String, String> map = new HashMap<>();
            map.put("name", name+"1");
            map.put("password", password+"1");
            return new ModelAndView(new MappingJackson2JsonView(), map);
        }
    }

    调用的时候 request.getParameter("name");获取的值一直是null;

    调试N次 最后发现是 contentType 的问题。真是无语!去掉  contentType 就可以获取到 data值了

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <script type="text/javaScript" src="${pageContext.request.contextPath }/js/jquery-3.6.0.min.js">
    </script>
    <body>
    
    <form name="Form2"  id="form2" action ="" method="post"  enctype="multipart/form-data"> 
     姓名:<input type="text" name="name" id="name">
     密码:<input type="text" name="password" id="password">
     <!-- 上传人:<input type="text" name="name">
     上传名:<input type="text" name="fileName"> -->
     <input type="button" value="测试" onclick="testJson()" />
    </form> 
    
    </body>
    <script type="text/javascript">
    function testJson() {
        alert("进来了");
        var name = $("#name").val();
        var password = $("#password").val();
        alert("姓名"+name+",学校:"+password);
        $.ajax({
            //请求路径
            url : "fileupload",
            //请求类型
            type : "post",
            //data表示发送的数据
            data :{
                    "name" : name,
                    "password" : password
                }, 
            //定义回调响应的数据格式为JSON字符串,该属性可以省略
            dataType : "json",
            //成功响应的结果
            success : function(data) {
                alert(data);
                if (data != null) {
                    alert("输入的用户名:" + data.name + ",密码:" + data.password);
                }
            }
        });
    }  
     
    
    </script>
    
    </html>
  • 相关阅读:
    在使用EF开发时候,遇到 using 语句中使用的类型必须可隐式转换为“System.IDisposable“ 这个问题。
    The OAuth 2.0 Authorization Framework-摘自https://tools.ietf.org/html/rfc6749
    Principles of good RESTful API Design 好的 RESTful API 设计
    JQuery发送Put、Delete请求
    一起学习 微服务(MicroServices)-笔记
    [WinForm] 使用 WebBrowser 操作 HTML 頁面的 Element-摘自网络
    关闭HTML5只能提示(form上新增novalidate)
    MVC5
    理解OAuth 2.0 -摘自网络
    Java三大主流开源工作流引擎技术分析
  • 原文地址:https://www.cnblogs.com/zjf6666/p/14623045.html
Copyright © 2011-2022 走看看