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. })

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


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

  • 相关阅读:
    CF Spreadsheets (数学)
    CF Theatre Square
    CF Error Correct System
    CF Playing with Paper
    HDU 3533 Escape (BFS + 预处理)
    nginx配置文件
    nginx配置文件详解
    Jenkins+maven+gitlab+shell实现项目自动化部署
    jenkins升级
    jenkins相关下载链接
  • 原文地址:https://www.cnblogs.com/jpfss/p/9323301.html
Copyright © 2011-2022 走看看