1

I do request using XMLHttpRequest to Java server but request params are missing.

So here is the js code which works fine (at least I see it sends params in network tab):

var data = JSON.stringify({
                dis: 'inline',
                ps: this.model.get('name')
            });

            var req = new XMLHttpRequest();
            req.open('POST', 'http://example.com/downloadPDF', true);
            req.responseType = 'arraybuffer';
            req.setRequestHeader("Content-type", "application/json; charset=utf-8");
            req.setRequestHeader("Content-length", data.length);

            req.onload = function (e) {
                //...
            };

            req.send(data);

And I get request without params on the server. Controller successfully stops here if to set breakpoint.

@RequestMapping(value = "downloadPDF", method = RequestMethod.POST, produces = "application/pdf")
    public void downloadPDF(HttpServletRequest request, HttpServletResponse response){
        String url = service + "/downloadPDF";
        log.debug(request.getParameterNames().size()) // size == 0
        List<NameValuePair> urlParameters = super.getParametersFromRequest(request);
        String browserType = request.getHeader("User-Agent");
        urlParameters.add(new BasicNameValuePair("browserType", browserType));
        super.readResponse(url, urlParameters, response);
    }

I've also caught request in the filter before doChain(), its params are also empty.

nllsdfx
  • 900
  • 14
  • 30
  • 1
    arguments to `.send` are ignored for request type `GET` or `HEAD` - [read documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) – Jaromanda X Feb 09 '17 at 08:36
  • `I also tried change type of the request to POST` - so, that should work, but now we need to see the server side code to see if you are `reading` the data correctly – Jaromanda X Feb 09 '17 at 08:37

0 Answers0