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

时间:2020-01-21 05:10:06

标签: javascript spring-boot xml-parsing xmlhttprequest

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

在这里,我将带有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 。我该如何解决?任何意见,帮助,指针欢迎!

2 个答案:

答案 0 :(得分:0)

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

&试试这个,而不是xhr.open("GET", url+articleId);

或尝试一下 xhr.open("GET", url+"?articleId="+articleId);

答案 1 :(得分:0)

我有正确的方法!现在正在工作。

客户端js文件

$scope.getAUGFile = function () {

                var xhr = new XMLHttpRequest();
                xhr.withCredentials = true;
                var url = RESOURCES.USERS_DOMAIN + "/AUGFile/excelDownload/"+articleId;
                xhr.open("GET", url);
                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("articleId") String 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));
        }
    }