我是新来的(对不起,我的英语不好。)我正在尝试从URL获取内容类型。我有以下代码:
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("HEAD", url, true);
xmlHttpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlHttpRequest.setRequestHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
xmlHttpRequest.setRequestHeader("Access-Control-Max-Age", "3600");
xmlHttpRequest.setRequestHeader("Access-Control-Allow-Credentials", "true");
xmlHttpRequest.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
xmlHttpRequest.onreadystatechange = function () {
if (this.readyState == this.DONE) {
var contentType = xmlHttpRequest.getResponseHeader("Content-Type");
console.log("CommonService.js", xmlHttpRequest.getResponseHeader("Content-Type"));
}
};
xmlHttpRequest.send();
在webConfig上,我有这个:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<system.webServer>
这是我在控制台日志中得到的错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:32100' is therefore not allowed access.
答案 0 :(得分:0)
我没有找到所发生情况的解决方案,所以我决定使用c#:
WebRequest request = WebRequest.Create(companyPoliticsDTO.PDFFileUrl); //generate 404
try
{
WebResponse response = request.GetResponse();
var contentType = response.ContentType;
return Ok(contentType);
}
catch (WebException ex)
{
HttpWebResponse errorResponse = ex.Response as HttpWebResponse;
}
这很好。