【发布时间】:2017-08-31 01:34:24
【问题描述】:
我正在从 Angular 调用 Web 服务
return $http.post(url_base 'appointment/export/', filter)
.then(handleSuccessHeaders, handleError('Error exporting file'));
function handleSuccessHeaders(data, status, headers, config) {
var res = {data:data, status:status, headers:headers,config:config};
return res;
}
这些服务用于导出 Excel 文件,如果我使用客户端,我无法下载文件(我看到标题),但我需要标题,因为我需要重要信息但始终未定义。
这是服务定义
@POST
@Path("/export/")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_OCTET_STREAM})
public Response exportInformation(AppointmentFilterDTO filter) {
String pathFile = "/home/files/test.xlsx"
File file = new File(pathFile);
if (file.exists()) {
java.nio.file.Path path = file.toPath();
return Response
.ok(new OutputStreamImplementation(path), MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "attachment; filename =" + path.getFileName().toString())
.header("Content-Length", file.length())
.header("success", true)
.header("contentType", "application/vnd.ms-excel")
.build();
} else {
return Response.ok()
.header("success", false)
.build();
}
}
所以我不知道我做错了什么,或者如果我遗漏了什么,我尝试从 data.headers() 获取标题,但它也不起作用
【问题讨论】:
-
传统上,
.then调用的函数只接收一个参数 - 你知道$http.post(不管是什么).then是用 4 个参数调用的吗?跨度> -
您正在使用 .then 就像 .success
.success(function(data, status, headers, config) {- 我相信您可以改为使用function handleSuccessHeaders({data, status, headers, config}) -
正如我所说,我尝试了一个参数,但是当我尝试执行 response.headers.name 之类的操作时,它说未定义
-
As I said I tried with one argument- 在哪里?无论如何,console.log 是一个参数,它看起来像什么 - 也可以在该函数中尝试 console.log(arguments.length) ,看看有多少参数被传递 -
{data: "symbols with data", status: 200, config: {...}, statusText: "OK", headers: ƒ}
标签: javascript angularjs rest http-headers jax-rs