【问题标题】:send the request id with XMLHttpRequest to the serverside将带有 XMLHttpRequest 的请求 id 发送到服务器端
【发布时间】:2020-05-07 03:00:14
【问题描述】:

这里我部署了带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文件(spring boot)

@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。我该如何解决?欢迎任何建议、帮助、指点!

【问题讨论】:

  • 您是否在客户端检查过 articleId 的值。您可以 console.log 并检查您是否获得了一些价值。
  • 您意识到您的请求将类似于/AUGFile/excelDownload/?{"articleID":123} - 您的服务器端会处理吗?
  • 我认为由于您的 url,在您的服务器端,您期望在 / -> /AUGFile/excelDownload/{articleId} 之后使用参数,而在 javascript/angularjs 中,您正在调用 api,参数为 ? -> @ 987654328@

标签: javascript spring-boot xml-parsing xmlhttprequest


【解决方案1】:

首先 console.log(articleId) 在您的函数中查看它是否已定义并可供 xhr 发送

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

或者试试这个 xhr.open("GET", url+"?articleId="+articleId);

【讨论】:

    【解决方案2】:

    我得到了正确的方法!它现在正在工作。

    客户端 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));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 2015-05-16
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2022-10-12
      相关资源
      最近更新 更多