ajax from HTTP to HTTPS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kristofo
    New Member
    • Mar 2014
    • 3

    #1

    ajax from HTTP to HTTPS

    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?

    Code:
    function test_kod(field) {
        var req = createXMLHTTPObject();
        if (!req) { 
            return false;
        };
        try {
            [B]//in IE <= 9 in this place debuger allways return error "access denied"[/B] 
            req.open("GET","https://dad-atlas.datasolutions.pl/karta.php?karta="+field.value,false);
        }
        catch(e){
            return false;
        }
        req.setRequestHeader('User-Agent','XMLHTTP/1.0');
        req.onreadystatechange = function () {
            if (req.readyState != 4) return;
            if (req.status != 200 && req.status != 304) {
                return false;
            }
            if (req.responseText == "TAK") {
                return true;
            } else {
                return false;
            };
        }
        if (req.readyState == 4) return;
        req.send();
    }
    
    var XMLHttpFactories = [
        function () {return new XMLHttpRequest()},
        function () {return new ActiveXObject("Msxml2.XMLHTTP")},
        function () {return new ActiveXObject("Msxml3.XMLHTTP")},
        function () {return new ActiveXObject("Microsoft.XMLHTTP")}
    ];
    
    function createXMLHTTPObject() {
        var xmlhttp = false;
        for (var i=0;i<XMLHttpFactories.length;i++) {
            try {
                xmlhttp = XMLHttpFactories[i]();
            }
            catch (e) {
                continue;
            }
            break;
        }
        return xmlhttp;
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • kristofo
      New Member
      • Mar 2014
      • 3

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

      Code:
          function test_kod(field) {
            var xhr = createCORSRequest('GET', "https://dad-atlas.datasolutions.pl/karta.php?karta="+field.value);
            //var xhr = createCORSRequest('GET', "http://facebook.com");
            if (!xhr) {
              alert('CORS not supported');
              return false;
            }
            // Response handlers.
            xhr.onload = function() {
              var text = xhr.responseText;
              var title = getTitle(text);
              alert('Response from CORS request to ' + url + ': ' + title);
            };
          
            xhr.onerror = function() {
              alert('Woops, there was an error making the request.');
            };
          
            xhr.send();
          }
          
          // Create the XHR object.
          function createCORSRequest(method, url) {
            var xhr = new XMLHttpRequest();
            if ("withCredentials" in xhr) {
              // XHR for Chrome/Firefox/Opera/Safari.
              xhr.open(method, url, true);
              alert("Firefox open");
            } else if (typeof XDomainRequest != "undefined") {
              // XDomainRequest for IE.
              xhr = new XDomainRequest();
              xhr.open(method, url);
              alert("IE open");
            } else {
              alert('CORS not supported');
              xhr = null;
            }
            return xhr;
          }
          
          // Helper method to parse the title tag from the response.
          function getTitle(text) {
            return text.match('<title>(.*)?</title>')[1];
          }

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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?

        Comment

        • kristofo
          New Member
          • Mar 2014
          • 3

          #5
          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.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            since I'm hardly using CORS I don't know either. some google searches will have to do.

            Comment

            Working...