简体   繁体   中英

POST parameters in Vue request to Java Servlet not getting through to Servlet?

I am learning Vue at the moment, and is trying to send some post variable onto a simple Java servlet. However, all the call for getParameter come out as NULL when I try to access them.

I send the post request with the following code:

    let formData = new FormData();
    formData.append('brukernavn',this.skjema.brukernavn);
    formData.append('passord',this.skjema.passord);
        console.log(formData);      
    axios.post('/BoardgamesRegister/ajax/login',formData)
    .then(function(response){
        console.log(response);
        })
    .catch(function(error){
    });

However, when I try to access these variable on the serverside with this code, I just get NULL as a value.

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException  {       
    
    System.out.println(req.getParameter("brukernavn"));
    System.out.println(req.getParameter("passord"));
    
    this.executeRequest(req,res,pw,null);
}

The thing is, if I attempt to add a GET parameter to the URL for the call, that value gets picked up correctly by the getParameter method. So it seems that my POST variables somehow get lost to the request. Is it something wrong with my Vue code or is it something on the serverside? I tried to Enumerate through the request parameters, and only the get parameters showed up, not the post ones.

It seems to be an issue in the Java Servlet, because when I tried to send the request to a PHP script instead, the POST paramentere were picked up correctly.

The comment from @Tim was not the issue, but it did indeed lead on the right path. I somehow missed that Axios encrypt post call parameters into JSON automatically. So in a normal POST request, getParameter will indeed work, but not here. So now, I simply need to find a way to stop Axios from converting into JSON.

Hmm. The way I usually do this is to have a class defined that is the DTO for the input data (Data Transfer Object) and I put it as a the argument (along with a @RequestBody annotation) of the method that is defined as the REST endpoint via @PostMapping.

@PostMapping("/thingDetail")
@ApiResponses(value = {
    @ApiResponse(code = 200, message = "Success"),
    @ApiResponse(code = 500, message = "Internal Server Error")
})
@ApiOperation(value = "Get detail information about a thing.")
public ThingDetailsDTO getThingDetails(@RequestBody ThingDetailsRequestDTO requestDto)
{
    return thingService.getDetails(requestDto);
}

However, if you're not using Spring Boot and annotations to control this, I suspect that you're operating at the wrong level. I think that you need to ask the HttpServletRequest.getParameter for "postData" or something like that, which is itself a map. I suggest that either you pause in the debugger and look at what's in the parameters portion of the req, or use printf to log what's in req.getParameters().

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