how to allow CORS for ASP.NET
<p>I have a problem trying to make a request from JS/Ajax to my WebAPI. In my solution I have a Web API that is published on srv02:2400 and my website that is published on srv02:2300</p>

<p>When I navigate to the page <a href="http://srv02:2300/all-requests.aspx" rel="nofollow noreferrer">http://srv02:2300/all-requests.aspx</a>, the page is loading fine except the data that is supposed to come form my API</p>

<p>and I am getting the error:</p>

<pre><code>XMLHttpRequest cannot load http://srv02:2400/api/requests/find/1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://srv02:2300' is therefore not allowed access. The response had HTTP status code 400.
</code></pre>

<p>However, If I take the url <a href="http://srv02:2400/api/requests/find/1" rel="nofollow noreferrer">http://srv02:2400/api/requests/find/1</a> and paste it in a browser, it is getting me the correct JSON list!.</p>

<p>I tried to enable the CORS globally as it is recommended <a href="https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#enable-cors" rel="nofollow noreferrer">here</a> in my API by:</p>

<ol>
<li>Installing the package Microsoft.AspNet.WebApi.Cors </li>
<li><p>Updating my WebApiConfig.cs by addding the following:</p>

<p>var cors = new EnableCorsAttribute("<a href="http://srv02:2400" rel="nofollow noreferrer">http://srv02:2400</a>", "<em>", "</em>");
config.EnableCors(cors);</p></li>
</ol>

<p>However, this did not work.</p>

<p>As I mentioned before, I am doing my request as Ajax reqeusts</p>

<pre><code>$.ajax({
        url: "http://srv02:2400/api/requests/find/" + id,
        type: "GET",
        dataType: 'json',
        contentType: 'application/json',
        success: function (requests) {
             console.log('success');
        },
        error: function (err) {
            if (err) {
                console.log(err);
            }
        }
    });
</code></pre>

<p>I tried to change json to jsonp as it is recommended <a href="https://stackoverflow.com/questions/14818498/how-to-allow-cors-access-control-allow-origin">here</a> but it did not work.</p>

<p>Any idea of how to solve the problem here. </p>

<p>Thank you!</p>