将一些PHP标头代码转换为经典ASP

时间:2020-12-30 01:40:52

I have 2 files:

我有2个文件:

  1. Locally hosted project coded in Ionic/AngularJS trying to retrieve JSON from the ASP file.
  2. 在Ionic / AngularJS中编码的本地托管项目试图从ASP文件中检索JSON。

  3. External ASP File which outputs JSON data made out of database values from a query.
  4. 外部ASP文件,用于输出由查询中的数据库值构成的JSON数据。

However, everytime my Angular code tries to call my externally hosted ASP file, I get the following error:

但是,每次我的Angular代码尝试调用外部托管的ASP文件时,都会收到以下错误:

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).

跨源请求已阻止:同源策略禁止在http://mysite/mobile/api.asp上读取远程资源。 (原因:缺少CORS标题'Access-Control-Allow-Origin')。

I understand that I probably didn't code my header wrongly somewhere. This is how I am currently setting my headers in Classic ASP:

我知道我可能没有在某处错误地编写我的标题。这就是我目前在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.

不幸的是,这不起作用,我甚至尝试在If语句之外执行Call AddHeader(“Access-Control-Allow-Origin”,“*”);但无济于事。

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.

现在这是PHP中的代码,我试图在ASP中模拟它。我设法成功检索JSON数据而不会出现头错误。

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.

那么为什么我不使用PHP文件呢?我当前的主机没有配置PHP并配置它,我将不得不重启服务器,这不是一个选项,因为我的网站每隔几秒就会发生连续的数据库更改。

I'm hoping someone can correct where I messed up the setting of Header in my ASP code.

我希望有人可以纠正我在ASP代码中搞砸了Header的设置。

1 个解决方案

#1


1  

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.

适用于$ http.get,它满足了我现在的需求。所以对于那些遇到$ http.post问题的人来说,可以试试$ http.get。

#1


1  

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.

适用于$ http.get,它满足了我现在的需求。所以对于那些遇到$ http.post问题的人来说,可以试试$ http.get。