1

When I try to retrieve my verifyCodeActual parameter on the server, the result is null.

String code = request.getParameter("verifyCodeActual"); // This is null

But I'm posting this parameter in the body as you can see in the chrome console data :

the chrome console (data form)

Here is the relevant part of the server-side java code:

Relevant part of the server-side java code

Here is the JS ajax query:

    var formData = new FormData();

    var verifyCodeActual = $('#j_captcha').val();

    formData.append("verifyCodeActual", verifyCodeActual);

    $.ajax({
        url : (isEdit ? editShopUrl : registerShopUrl),
        type : 'POST',
        data : formData,
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        processData: false,
        success ...
        }
    });
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
Heiku
  • 11
  • 3
  • if my description does not clear,please tell me – Heiku May 20 '18 at 12:24
  • 1
    Welcome to Stack Overflow! Could you add the relevant part of your Java code? Instead of tyhe image. – Turtle May 20 '18 at 12:27
  • I've not done a great deal with using a FormData object but if what I read is correct, isn't the JSON structure being received by the server more like **{"form": { "verifyCodeActual":"somevalue"}, "args":{}, etc}** meaning that you need to first fetch the **form** value then get the **verifyCodeActual** from that? – Jere May 20 '18 at 13:46
  • it is my first time to use formData,i don't know how to get the form value or property – Heiku May 20 '18 at 14:00

2 Answers2

0

I suspect this has something to do with your java code which doesn't even look like a servlet. You have no doPost method there. What IDE are you using?

Here's what your servlet should look like:

@WebServlet("/modifyShop")
public class ModifyShop extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ModifyShop() {
        super();
    }

    protected void doPost(HttpServletRequest request,HttpServletResponse response){

        String code = request.getParameter("verifyCodeActual");
        System.out.println(code);

    }

}

Also since you are using Jquery, you can create ajax methods with Jquery which are generally simpler. There are many different ways you can send ajax requests with Jquery and i highly recommend checking out BalusC's answer here about how to use Servlets and Ajax.

Here's an example where you can manually do it:

var verifyCodeActual = $('#j_captcha').val();
var params = {
    verifyCodeActual: verifyCodeActual
};

$.post("modifyShop", $.param(params), function(response) {
   //handle response here if you have any
});
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
0

This will help you

Client

$.ajax({
    url : (isEdit ? editShopUrl : registerShopUrl),
    type : 'POST',
    data : {
        verifyCodeActual : $('#j_captcha').val()
    },
    contentType: "application/x-www-form-urlencoded",
    cache: false,
    processData: false,
    success ...
    }
});

Controller

@RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> modifyShop(@RequestBody Object verifyCodeActual) throws Exception {

        System.out.println(verifyCodeActual);
}
Devratna
  • 938
  • 1
  • 7
  • 26