简体   繁体   English

Vue 请求中的 POST 参数到 Java Servlet 没有通过 Servlet?

[英]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.我目前正在学习 Vue,并试图将一些 post 变量发送到一个简单的 Java servlet。 However, all the call for getParameter come out as NULL when I try to access them.但是,当我尝试访问它们时,所有对 getParameter 的调用都以 NULL 的形式出现。

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.但是,当我尝试使用此代码在服务器端访问这些变量时,我只是将 NULL 作为值。

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.问题是,如果我尝试将 GET 参数添加到 URL 以进行调用,则 getParameter 方法会正确获取该值。 So it seems that my POST variables somehow get lost to the request.因此,我的 POST 变量似乎以某种方式丢失了请求。 Is it something wrong with my Vue code or is it something on the serverside?我的 Vue 代码有问题还是服务器端有问题? 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.这似乎是 Java Servlet 中的一个问题,因为当我尝试将请求发送到 PHP 脚本时,POST 参数被正确提取。

The comment from @Tim was not the issue, but it did indeed lead on the right path. @Tim 的评论不是问题,但它确实引领了正确的道路。 I somehow missed that Axios encrypt post call parameters into JSON automatically.我不知何故错过了 Axios 自动将调用后参数加密到 JSON 中。 So in a normal POST request, getParameter will indeed work, but not here.所以在正常的 POST 请求中,getParameter 确实可以工作,但在这里不行。 So now, I simply need to find a way to stop Axios from converting into JSON.所以现在,我只需要找到一种方法来阻止 Axios 转换为 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.我通常这样做的方法是定义一个 class ,它是输入数据(数据传输对象)的 DTO,我把它作为定义为 REST 的方法的参数(连同 @RequestBody 注释)通过@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.但是,如果您没有使用 Spring 引导和注释来控制它,我怀疑您的操作级别错误。 I think that you need to ask the HttpServletRequest.getParameter for "postData" or something like that, which is itself a map.我认为您需要向 HttpServletRequest.getParameter 询问“postData”或类似的东西,它本身就是 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().我建议您在调试器中暂停并查看 req 的参数部分中的内容,或者使用 printf 记录 req.getParameters() 中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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