Sun*_*nil 2 content-type angularjs
我有以下 Angular 代码,用于调用返回字节数组的 Web API。我想将 ContentType 设置为“application/pdf”,这在 jQuery ajax 调用中非常简单,但无法找到在 angularjs 中执行此操作的方法。
问题:我该如何在 angularjs 中执行以下代码?
return $http.get('/api/v1/alarms/exportsummarytopdf?start=2011-03-
30T05:00:00.000Z&end=2015-04-14T05:00:00.000Z')
.then(getSummaryPdfExportComplete)
.catch(function (message) {
exception.catcher('XHR failed for getSummaryPdfExport')(message);
});
function getSummaryPdfExportComplete(response) {
logger.info('getSummaryPdfExport: complete');
return response.data;
}
Run Code Online (Sandbox Code Playgroud)
更新1:
当我直接访问类似的 URL 时,Chrome 会清楚地将内容类型显示为“application/pdf”。调用返回字节数组的 Web api 时,我可能需要使用其他内容。

Chrome 中显示的错误如下。

当我使用 responseType: 'arraybuffer' 时,此失败请求的标头如下。

对基本问题的简短回答是,如果您想在 $http.get() 调用中设置标头,请将其作为“config”(第二个可选参数)的一部分发送。
例如:
$http.get('/api/v1/alarms/exportsummarytopdf?start=2011-03-
30T05:00:00.000Z&end=2015-04-14T05:00:00.000Z',
{headers: { 'Accept': 'application/pdf' });
Run Code Online (Sandbox Code Playgroud)
对于您的具体问题,您似乎想要“接受”而不是“内容类型”。
有关详细信息,请参阅HTTP 标头。