0

My Ajax code looks like this

$.ajax({
    type : 'post',
    url : url,
    async : false,  
    data: {'txToClose': '1234,5678','sno':'0195'},
    contentType : "application/x-www-form-urlencoded; charset=utf-8",
    dataType : "json",      
    success : function(result,status,xhr){
        console.log(result);

    }                           
});

The txToClose value 1234,5678 will be coming from a textbox as comma separated string. The user will type them as comma separated.

Im trying to receive them in controller as

@PostMapping("/txToClose")  
public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response) throws BBException
{
    logger.info("Called txToClose controller");
    ResultDto resultDto = new ResultDto();

    String txToClose= request.getParameter("txToClose");
    String sno= request.getParameter("sno");

    logger.info("Transactions to close :"+txToClose+", Serial Num :"+sno);
}

Output is

Transactions to close :null, Serial Num :null

What am I missing here?

Yakhoob
  • 469
  • 1
  • 10
  • 24

3 Answers3

1

You're posting your data as request body, which of course using request.getParameter() will give you null. getParameter() was used to retrieve value from URL parameter.

I'm not quite experience with SpringMVC but try to change your method parameter into

public ResultDto txToClose(@RequestBody ResultDto resultDto ) throws BBException
{
    logger.info("Called txToClose controller");

    String txToClose= resultDto.getTxtToClose();
    String sno= resultDto.getSno();

    logger.info("Transactions to close :"+txToClose+", Serial Num :"+sno);
}

I assume your ResultDto is same as your JSON format.

Sukma Wardana
  • 468
  • 6
  • 16
  • you mean my ResultDto should have txToClose & sno? – Yakhoob Oct 12 '17 at 08:47
  • Yes, and if you don't want to mapping your JSON into ResultDto, you can check this discussion: https://stackoverflow.com/a/12897632/5852226 – Sukma Wardana Oct 12 '17 at 08:49
  • It worked when I have my signature as public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response,@RequestBody ObjectNode json) – Yakhoob Oct 12 '17 at 08:57
0

remove the line

contentType : "text/plain; charset=utf-8",

or use default contentType

contentType : "application/x-www-form-urlencoded; charset=utf-8",

It should work.

  • Just to debug, try once with @RequestMapping(value="/txToClose") @ResponseBody public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response) throws BBException { logger.info("Called txToClose controller"); ResultDto resultDto = new ResultDto(); String txToClose= request.getParameter("txToClose"); String sno= request.getParameter("sno"); logger.info("Transactions to close :"+txToClose+", Serial Num :"+sno); } – Bikramjit Rajbongshi Oct 12 '17 at 08:57
  • Solved it. By adding ,@RequestBody ObjectNode json – Yakhoob Oct 12 '17 at 08:57
0

I solved the issue by adding @RequestBody ObjectNode json in the signature.

public ResultDto txToClose(HttpServletRequest request, HttpServletResponse response,@RequestBody ObjectNode json) throws BBException
{
    logger.info("Called txToClosecontroller");
    ResultDto result = new ResultDto();

    String txToClose= json.get("txToClose").asText();
}
Yakhoob
  • 469
  • 1
  • 10
  • 24