By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

ajax from HTTP to HTTPS

P: 3
On my page I have the FORM with one INPUT field. On SUBMIT this form I use JavaSript function "test_kod(this)" to valid is the the enetered value correct or not. But in the Internet Explorer ver <=9 the OPEN method allways fall with error "access denied".
WHAT AM I DOING WRONG?

Expand|Select|Wrap|Line Numbers
  1. function test_kod(field) {
  2.     var req = createXMLHTTPObject();
  3.     if (!req) { 
  4.         return false;
  5.     };
  6.     try {
  7.         //in IE <= 9 in this place debuger allways return error "access denied" 
  8.         req.open("GET","https://dad-atlas.datasolutions.pl/karta.php?karta="+field.value,false);
  9.     }
  10.     catch(e){
  11.         return false;
  12.     }
  13.     req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  14.     req.onreadystatechange = function () {
  15.         if (req.readyState != 4) return;
  16.         if (req.status != 200 && req.status != 304) {
  17.             return false;
  18.         }
  19.         if (req.responseText == "TAK") {
  20.             return true;
  21.         } else {
  22.             return false;
  23.         };
  24.     }
  25.     if (req.readyState == 4) return;
  26.     req.send();
  27. }
  28.  
  29. var XMLHttpFactories = [
  30.     function () {return new XMLHttpRequest()},
  31.     function () {return new ActiveXObject("Msxml2.XMLHTTP")},
  32.     function () {return new ActiveXObject("Msxml3.XMLHTTP")},
  33.     function () {return new ActiveXObject("Microsoft.XMLHTTP")}
  34. ];
  35.  
  36. function createXMLHTTPObject() {
  37.     var xmlhttp = false;
  38.     for (var i=0;i<XMLHttpFactories.length;i++) {
  39.         try {
  40.             xmlhttp = XMLHttpFactories[i]();
  41.         }
  42.         catch (e) {
  43.             continue;
  44.         }
  45.         break;
  46.     }
  47.     return xmlhttp;
  48. }
  49.  
Mar 30 '14 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
WHAT AM I DOING WRONG?
you’re not adhering to the SOP.

you’d either have to call a server script that requests the resource or you enable CORS.
Mar 30 '14 #2

P: 3
I digged in the NET and changed my script to use the XDomainRequest. It works good, but only if the url request has HTTP protocol. When I try to use url request with HTTPS protocol, in IE I still have the "access denied" error.

My new script.

Expand|Select|Wrap|Line Numbers
  1.     function test_kod(field) {
  2.       var xhr = createCORSRequest('GET', "https://dad-atlas.datasolutions.pl/karta.php?karta="+field.value);
  3.       //var xhr = createCORSRequest('GET', "http://facebook.com");
  4.       if (!xhr) {
  5.         alert('CORS not supported');
  6.         return false;
  7.       }
  8.       // Response handlers.
  9.       xhr.onload = function() {
  10.         var text = xhr.responseText;
  11.         var title = getTitle(text);
  12.         alert('Response from CORS request to ' + url + ': ' + title);
  13.       };
  14.  
  15.       xhr.onerror = function() {
  16.         alert('Woops, there was an error making the request.');
  17.       };
  18.  
  19.       xhr.send();
  20.     }
  21.  
  22.     // Create the XHR object.
  23.     function createCORSRequest(method, url) {
  24.       var xhr = new XMLHttpRequest();
  25.       if ("withCredentials" in xhr) {
  26.         // XHR for Chrome/Firefox/Opera/Safari.
  27.         xhr.open(method, url, true);
  28.         alert("Firefox open");
  29.       } else if (typeof XDomainRequest != "undefined") {
  30.         // XDomainRequest for IE.
  31.         xhr = new XDomainRequest();
  32.         xhr.open(method, url);
  33.         alert("IE open");
  34.       } else {
  35.         alert('CORS not supported');
  36.         xhr = null;
  37.       }
  38.       return xhr;
  39.     }
  40.  
  41.     // Helper method to parse the title tag from the response.
  42.     function getTitle(text) {
  43.       return text.match('<title>(.*)?</title>')[1];
  44.     }
  45.  
  46.  
Mar 30 '14 #3

Dormilich
Expert Mod 5K+
P: 8,639
When I try to use url request with HTTPS protocol, in IE I still have the "access denied" error.
there’s probably not much to do then. does it work in other browsers?
Mar 31 '14 #4

P: 3
Yes, it works good in Firefox and IE ver > 9, But in IE <=9 I still have the "access denied" error.
I heard about same IFRAME method to get around this problem, but I don't know how to do it.
Mar 31 '14 #5

Dormilich
Expert Mod 5K+
P: 8,639
since I'm hardly using CORS I don't know either. some google searches will have to do.
Apr 1 '14 #6

Post your reply

Sign in to post your reply or Sign up for a free account.