Reputation: 118
I have 2 files:
However, everytime my Angular code tries to call my externally hosted ASP file, I get the following error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://mysite/mobile/api.asp. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
I understand that I probably didn't code my header wrongly somewhere. This is how I am currently setting my headers in Classic ASP:
If Request.ServerVariables("HTTP_ORIGIN") <> Null Or Request.ServerVariables("HTTP_ORIGIN") <> "" Then
Call AddHeader("Access-Control-Allow-Origin", Request.ServerVariables("HTTP_ORIGIN"))
Call AddHeader("Access-Control-Allow-Credentials", "true")
Call AddHeader("Access-Control-Max-Age", "86400")
End If
If Request.ServerVariables("REQUEST_METHOD") = "OPTIONS" Then
Call AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
Call AddHeader("Access-Control-Allow-Headers", Request.ServerVariables("HTTP_ACCESS_CONTROL_REQUEST_HEADERS"))
Call Response.AddHeader("Access-Control-Allow-Origin", "*")
End If
This does not work unfortunately, I've even tried just doing Call AddHeader("Access-Control-Allow-Origin", "*")
outside of the If
statement; but to no avail.
Now this is the code in PHP which I am trying to emulate in ASP. I managed to retrieve JSON data successfully without getting header errors.
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
So why don't I just use the PHP file if it works? My current host does not have PHP configured and to configure it, I would have to restart the server which is not an option since my site has continous database changes happening every few seconds.
I'm hoping someone can correct where I messed up the setting of Header in my ASP code.
Upvotes: 1
Views: 756
Reputation: 118
I found a workaround to this. I noticed that
Call Response.Addheader("Access-Control-Allow-Origin", "*")
works for $http.get
and it's fulfilling my need for now. So for those having trouble with $http.post
can give $http.get
a try.
Upvotes: 1