Amalina Aziz
Amalina Aziz

Reputation: 204

JS variables across domains

Problem

I want to receive javascript variables from another HTTPS domain but I got error.

What I've Tried

This is the most simplified version of my code that I've been able to get, which still produces the problem I described above.

 $.ajax({
          url: "https://www.example.com/page.asp",
          crossDomain:true,
          success:function(data){
                alert("success")
          },
          error : function() {
             alert("error")
          }
      });

and I also tried

$.ajax({
          url: "https://www.example.com/page.asp",
          dataType: "jsonp",
          cache: true,
          success:function(data){
                alert("success")
          },
          error : function(XMLHttpRequest, textStatus, errorThrown) {
             alert("error")
          }
      });

and the codes below also added in the remote page

<%Response.AddHeader "Access-Control-Allow-Origin","*"%>

It does not work because it gives the following error:

'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

Question

So my basic question is, how do I receive javascript variables from HTTPS page to HTTP page?

Upvotes: 0

Views: 703

Answers (2)

Amalina Aziz
Amalina Aziz

Reputation: 204

I finally managed to receive javascript variables with the following codes:

$.ajax({
   method: "GET",
   url: "http://www.example.com/test.js",
   dataType: "script",
   success:function(data){
     alert(variable)
   }
});

Thank you.

Upvotes: 0

Ciccio
Ciccio

Reputation: 468

Have you tried to set in web.config? I use it only with asp.net web api, and in it there is a dll for manage cors.

<httpProtocol>
        <customHeaders>
            <clear />
            <add name="X-Powered-By" value="ASP.NET" />
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time"/>
                <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
                <add name="Access-Control-Allow-Credentials" value="true"/>
        </customHeaders>
        <redirectHeaders>
            <clear />
        </redirectHeaders>
    </httpProtocol>

Upvotes: 1

Related Questions