zoukankan      html  css  js  c++  java
  • 关于后台接收参数为null的问题之ajax--contentType

    ajax方法中的参数:
    contentType:发送至服务器时内容的编码类型,一般默认:application/x-www-form-urlencoded(适应大多数的场合)
    dataType:预期服务器返回的数据类型

    有时候前台ajax向后台传数据的时候,能够进到后台,但是后台显示接受的参数为null,如果ajax没有问题,这个时候就要考虑后台在接收参数的时候使用的是什么注解。(@RequestParam还是@RequestBody)
        public HttpResponseEntity selectByCollege(@RequestParam Map<String,Object> collegeName) {
    使用@RequestParam:(推荐这个)
    contentType:"application/x-www-form-urlencoded"
    data:{'college',college}
    使用@RequestParam,可以不用写contentType,
    application/x-www-form-urlencoded是contentType的默认值
    使用@RequestBody时:
     接收的是json字符串格式的数据,
     需要将contentType写成:'application/json',
     data:Json.Stringly(da)(将对象变成字符串)
     let da = {'state':state};
    1. $.ajax({
    2. type: "POST",
    3. url: httpUrl + "/insertInfo",
    4. dataType: 'json',
    5. data: JSON.stringify(da),
    6. contentType: "application/json",
    7. success: function (result) {
    8. console.log(result);
    9. $('#ModalInfo').modal('hide');
    10. },
    11. error: function () {
    12. console.log('错误')
    13. }
    14. })

    这样就可以解决后台接不到参数的问题啦!!!!


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    mysql的安装
    一个电脑登录多个微信
    项目工程构建
    MYSQL 添加字段
    Centos 搭建maven私服
    Nacos 动态刷新@RefreshScope
    Cookie & Session
    阿里蚂蚁 笔试题
    springboot 将配置文件中的配置读取为properties配置类
    .Net Gacutil工具(全局程序集缓存工具)使用教程
  • 原文地址:https://www.cnblogs.com/jpfss/p/9323301.html
Copyright © 2011-2022 走看看