Configuring IIS to allow CORS requests
The problem: sending ajax requests from HTML5 application on local machine to my API that hosted under IIS on Google Cloud.
The facts:
The code looks like:
$.post({
url: apiEndpoint + "Init",
contentType: "application/json; charset=utf-8",
data: data,
success: function (response) {
logResponse("API response [Init]", response.d);
},
error: function (xhr, status, error) {
logResponse('Fail to call init API', data);
}
});
The facts:
The code looks like:
$.post({
url: apiEndpoint + "Init",
contentType: "application/json; charset=utf-8",
data: data,
success: function (response) {
logResponse("API response [Init]", response.d);
},
error: function (xhr, status, error) {
logResponse('Fail to call init API', data);
}
});
The error was looks like:
Originally, there was "crossDomain: true" parameter in ajax request but it doesn't help.
There were crossdomain.xml and clientaccesspolicy.xml on the server and they were helpless as well.
The way:
I went to internet for advice and saw many and many solutions for ajax - nothing doesn't help.
The way:
I went to internet for advice and saw many and many solutions for ajax - nothing doesn't help.
What I tried:
- add to ajax
headers: {
'Access-Control-Allow-Origin': '*'
}
- add on server
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
But after that change on the server, the error was looks like:
So I added
<add name="Access-Control-Allow-Headers" value="Content-Type" />
to <system.webServer> <httpProtocol> <customHeaders> section.
The conclusion: No need to add nothing to ajax request - not headers, not even "crossDomain: true". Just add to web.config <system.webServer> section:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
The conclusion: No need to add nothing to ajax request - not headers, not even "crossDomain: true". Just add to web.config <system.webServer> section:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
That's it. End of story. Unfortunately - the long story :-\
Comments
Post a Comment