【发布时间】: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