【问题标题】:Send a data through Ajax, and the background never receives it通过ajax发送一个数据,后台从来没有收到
【发布时间】:2020-06-24 10:02:20
【问题描述】:

前台通过Ajax向后台发送ID,但后台从不接收。我被这个问题困扰了一整天,真的需要你的帮助。这是我的JS和Controller以及错误信息

$('.category-wrap').on('click', '.now .delete', function (e) {
        var target = e.currentTarget;
        var pc = target.dataset.id;
        var pcId = {'pcId':pc};
        $.confirm('sure?',function () {
            $.ajax({
                url: deleteUrl,
                type: 'POST',
                data:  pcId,
                contentType: 'application/json',
                cache: false,
                success: function (data) {
                    if (data.success) {
                        $.toast('successfully delete!');
                        getList();
                    } else {
                        $.toast('Delete failed!');
                    }
                }
            });
        })
    });
@RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> removeProductCategory(@RequestBody Integer pcId,
                                                  HttpServletRequest request) 
{...}

image 1
image 2

【问题讨论】:

    标签: java ajax


    【解决方案1】:

    您发送一个 json 请求 var pcId = {'pcId':pc}; 并尝试接收一个整数 @RequestBody Integer pcId

    尝试像这样定义一个 Pojo

    class RequestData {
        public Integer pcId;
    }
    

    并修改控制器方法参数

    @RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST, consumes = "application/json" )
    @ResponseBody
    public Map<String, Object> removeProductCategory(@RequestBody RequestData pcId,
                                                      HttpServletRequest request)
    

    【讨论】:

    • 非常感谢。事实上,前台需要做出改变。发送的数据应该是JSON格式的,所以前台只需要加上下面一句——“JSON.stringify(pcId)”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2016-10-17
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2019-02-07
    相关资源
    最近更新 更多