0

i am new in this(and my english is bad, sorry). I am trying to get the content type from the url. I have this code:

 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();

on the webConfig, i have this:

  <system.webServer>
<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  <system.webServer>

and this is the error i am getting, in the console log:

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.

1 Answers1

0

i didn't find a solution of what was happening, so i decided to use 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;

        }

this work fine.