Node.js http和https响应

时间:2018-09-18 09:19:36

标签: javascript html http https xmlhttprequest

我从某个页面获取XMLHttpRequest。 ...

   var xhr = new XMLHttpRequest();
   xhr.withCredentials = true;
   if ("withCredentials" in xhr) {
     // XHR for Chrome/Firefox/Opera/Safari.
     xhr.open('GET', 'http://localhost/some_request', true);
   } else (typeof XDomainRequest != "undefined") {
     // XDomainRequest for IE.
     xhr = new XDomainRequest();
     xhr.open('GET', 'http://localhost/some_request');
   }

   xhr.onload = function() {
        var text = xhr.responseText;
        alert(text);
   };
   ... 

问题是,当我从https发送请求时,它不起作用。使服务器将响应发送到“ https://localhost/ ...”,而不是“ http://localhost/ ...”,是否有办法说出脚本我正在使用哪种协议?例如:

if(...)
    xhr.open('GET', 'http://localhost/some_request'); //when i'm using http
else
    xhr.open('GET', 'https://localhost/some_request'); //when i'm using https

我应该使用window.location.protocol吗?

0 个答案:

没有答案