简体   繁体   中英

How to pass data from Spring controller to JQuery $.post() call

After posting data from the Jquery $.post() method, I am attempting to return some data which I want to use in the Jquery alert() from a Spring boot 1.5.1 controller method. Currently, the returned data is empty when used in the alert().

Jquery:

$('#element').click(function() {
    var formData = $('#element').serialize();

    var posting = $.post( '/update.json?id=${item.id}', formData );
    posting.done(function( data ) {
        alert(data);
    });

    return false;
});

Controller:

@RequestMapping("/update.json")
@ResponseBody
public void update(HttpServletRequest request, 
        Map model, 
        @RequestParam(value="id", required=true) Integer id) {

    // to validation and binding...

    model.put("result", "test");
}

Why is the result property not accessible in the Jquery data object in the callback?

Your Controller has a return type as void, that's normal. You can either use a response entity, if you want to set the status:

return new ResponseEntity<String>("test", HttpStatus.OK)

or you can return a plain string a well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM