Ajax message for spring mvc adds an "=" sign to request data

I am trying to send data though ajax message to spring controller. My ajax code

function postData(tag){
console.debug(tag);

var targetUrl = "/add/tag";
$.ajax({
    url : targetUrl,
    type : "POST",
    data : tag,
    dataType : "text",
    success : function(response){
        console.debug(response);
    },
    error : function(){
        console.debug("error : ".concat(response));
    }
});
}

and my controller code

@RequestMapping(value = "/add/tag", method = POST, consumes = { "application/json" },headers = "content-type=application/x-www-form-urlencoded")
@ResponseBody
public Integer addTag(HttpServletRequest request,
    @PathVariable("uid") String gatheringUid, @RequestBody String tag) {
    System.out.print(tag);
    return gatheringService.updateGathering(gatheringUid, tags);
}

on the server side, it prints the value of the tag added by the "=" sign, while the value of the firebug column is displayed as I enterd.

For example, when I send test data, it prints “test” on the firebug console, and on the server side it prints “test =”.

Can someone tell me what the problem is?

Thanks in advance, Regards.

+5
source share
2 answers

, AJAX POST application/x-www-form-urlencoded.

Spring StringHttpMessageConverter @RequestBody String. , POST. , , . text , , , <input> , .. text=.

, ServletServerHttpRequest#getBodyFromServletRequestParameters(..).

- , , text/plain. dataType. contentType headers.

+5

, , Ajax jQuery .

     $.ajax({
            type : "post",
            dataType : 'json', 
            contentType : 'text/plain', // This was added to delete the =
            url : 'myURL',  
            data : id
     })
0

Source: https://habr.com/ru/post/1528894/


All Articles