将带有XMLHttpRequest的请求ID发送到服务器端

问题描述 投票:0回答:1

感谢您阅读我的问题。请足够支持我!

在这里,我将带有ID的Get请求部署为从客户端传递到服务器端,以根据ID下载Excel文件。

客户端js文件

$scope.getAUGFile = function () {

                var xhr = new XMLHttpRequest();
                xhr.withCredentials = true;
                var params = JSON.stringify({ articleId: articleId });
                var url = RESOURCES.USERS_DOMAIN + '/AUGFile/excelDownload/'
                xhr.open("GET", url+"?"+params);
                xhr.setRequestHeader("authorization", getJwtToken());
                xhr.responseType = 'blob';

                xhr.onload = function () {
                    if (this.status === 200) {
                        saveAs(xhr.response, "mvvAUGExcelTemplate.xls");
                    }
                };

                xhr.send(null);
            };

服务器端js文件(春季启动)

@RequestMapping(value = "/AUGFile/excelDownload/{articleId}", method = RequestMethod.GET)
    public ResponseWrapper excelGenerateAUG(HttpServletRequest request, HttpServletResponse response,@PathVariable Long articleId){

        try{
            fileService.downloadAUGFile(request,response,articleId);
            return ResponseWrapper.successWithMessage(messageSource.getMessage("success_code",null, Locale.ENGLISH));
        } catch (Exception e){
            lOG.error(">> Excel file Download error", e);
            return ResponseWrapper.failWithMessage(messageSource.getMessage("fail_code", null, Locale.ENGLISH));
        }
    }

执行客户端功能时,在服务器端将articleId值设为NULL。我该如何解决?任何意见,帮助,指针欢迎!

javascript spring-boot xml-parsing xmlhttprequest
1个回答
0
投票

第一console.log(articleId)在您的函数内部,以查看其是否已定义并可供xhr发送[]

&尝试用这个代替xhr.open("GET", url+articleId);

© www.soinside.com 2019 - 2024. All rights reserved.