简体   繁体   中英

Set Up CORS IIS7

I have a webforms site and I am trying to make a web request via JQuery to a page on the same site. However the page I am trying to access is on HTTPS rather than the HTTP that the page the Jquery is executed from uses. I get an error regarding "Access-Control-Allow-Origin" so I looked into it and I now have the following added to my web.config.

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

And this is my JQuery

<script type="text/javascript">
    $( document ).ready(function() {
        $.ajax({
            type: "GET",
            headers: {
                "Access-Control-Allow-Origin": "*"
            },
            url: "https://www.example.com",
            success: function (resp) {
                //register browser as supported
                console.log("supported");
            },
            error:function(e){
                //register browser as unsupported
                console.log(e);
                console.log("unsupported");
            }
        })
    })
</script>

However I now get the following error

XMLHttpRequest cannot load https://www.example.com/ . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://www.example.com ' is therefore not allowed access.

What do I need to do to get this to work?

I have realised what was happening. We have a strange setup where there is a CMS system that is the root site but the rest of the system is its own webforms application. The CMS systems web.config gets called first so that is where the changes needed to be made but I had made them in the main sites web.config.

Silly setup....

Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM