【问题标题】:CORS request in Internet ExplorerInternet Explorer 中的 CORS 请求
【发布时间】:2014-08-21 22:53:35
【问题描述】:

我需要使用应该在 Internet Explorer 中工作的 Javascript 发出一个发布请求。客户端和服务器位于不同的服务器上并使用不同的协议。

我尝试了两种不同的方法

1)

                    var xdr = new XDomainRequest();
                    var user = document.getElementById('username').value;
                    var psw = document.getElementById('password').value;
                    var params = "username="+user+"&password="+psw+"&system=telenetweb";
                    xdr.open("post", url);

                        xdr.onprogress = function () {
                        //Progress
                      };

                      xdr.ontimeout = function () { 
                        //Timeout
                      };

                      xdr.onerror = function () { 
                        //Error Occured
                      };

                      xdr.onload = function() {
                        //success(xdr.responseText);
                      }

                      setTimeout(function () {
                        xdr.send(params);
                      }, 0);

但它不起作用,因为我正在从 http 向 https 发出请求。

2)

            var http;
            try {
                http = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                  http = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                  http = false;
                }
            }
              if (!http && typeof XMLHttpRequest!='undefined') {
                http = new XMLHttpRequest();
            }

            var user = document.getElementById('username').value;
            var psw = document.getElementById('password').value;
            var params = "username="+user+"&password="+psw+"&system=telenetweb";

            http.open("POST", url, true);
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            http.setRequestHeader("Content-length", params.length);
            http.setRequestHeader("Connection", "close");
            http.send(params);

这在 Chrome 中有效,但在 IE 中无效,因为 IE 不会将 Origin 标头附加到请求中。

我该如何解决这个问题?

【问题讨论】:

    标签: internet-explorer cross-domain cors


    【解决方案1】:

    MSIE 的行为显然取决于版本。我不建议你使用XDomainRequest, because it is very limited,所以不值得努力。 XHR2 is supported from MSIE version 10,你应该使用它,它的工作方式与 chrome 或 firefox 完全相同。如果您想要使用较低 MSIE 版本的 CORS,我认为您应该忘记它。

    如果你真的需要低版本的 MSIE(我怀疑)你可以使用 JSONP,它也有限制,但也许它可以解决你的问题。

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2012-05-10
      • 2013-08-12
      • 2014-10-19
      • 2013-03-07
      • 1970-01-01
      相关资源
      最近更新 更多