在角度应用中发布解析Content-Range标头的问题(返回null) [英] Issue parsing Content-Range header in angular app (returns null)
问题描述
我的.NET API服务器正在将一些数据返回到Angular客户端,并且它设置 Content-Range
标头,如下所示:
My .NET API server is returning some data to my Angular client and it sets the Content-Range
header like so:
var response = await base.ExecuteAsync(cancellationToken);
const string unit = "items";
response.Headers.Add("Accept-Ranges", unit);
response.Headers.Add("Range-Unit", unit);
response.Content.Headers.ContentRange = new ContentRangeHeaderValue(_requestedFrom, _requestedTo, _totalCount)
{
Unit = unit
};
这似乎工作正常,如我的网络标签所示: Content-Range:items 0-9/122
And this is seemingly working fine, as my network tab shows: Content-Range:items 0-9/122
我遇到的问题是,当我尝试解析此数据时,解析似乎失败了:
The problem that I have is, when I attempt to parse this data, the parsing seemingly fails:
var response = parseRange(headers('Content-Range'));
console.log("response " + response);
控制台记录:响应为空
.有什么想法我做错了吗?
The console logs: responce null
. Any ideas what I am doing wrong?
编辑. parseRange
函数
function parseRange(hdr) {
var m = hdr && hdr.match(/^(?:items )?(\d+)-(\d+)\/(\d+|\*)$/);
if(m) {
return {
from: +m[1],
to: +m[2],
total: m[3] === '*' ? Infinity : +m[3]
};
} else if(hdr === '*/0') {
return { total: 0 };
}
return null;
}
此函数中的
hdr
为空,因此出现 headers()
函数是问题吗?
hdr
in this function is null, so it appears the headers()
function is the issue?
推荐答案
这里的问题实际上是在服务器端,我需要将内容范围添加到公开的标头中:
The issue here was actually on the server side, I needed to add content-range to the exposed headers:
[EnableCors("http://localhost:3000", "*", "*", exposedHeaders: "Content-Range")]
这篇关于在角度应用中发布解析Content-Range标头的问题(返回null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!