CORS与IE浏览器,XMLHtt prequest和SSL(HTTPS) [英] CORS with IE, XMLHttpRequest and ssl (https)

查看:435
本文介绍了CORS与IE浏览器,XMLHtt prequest和SSL(HTTPS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,即通信通阿贾克斯从另一台服务器消耗一定的RESTful服务的应用程序。

I've a problem with an application that is communicating thru Ajax to consume some restful services from another server.

问题接缝只是与IE浏览器出现,当消费应用程序是在一个服务器有SSL,我的意思是,当我从其他箱子都在同一台服务器消耗相同的服务工作正常。

The problem seams to appear just with IE and when the consuming application is in a Server that have ssl, I mean when I consume the same services from the same server from other box everything works fine.

我有一个服务器上的RESTful服务(的https://api.restbla/ .. )有SSL 我已经来消费这些从其他服务器(带或不带SSL证书)

I've the Restful services on a Server (https://api.restbla/..) that have SSL and I've to consume these from other servers (with or without ssl certificate)

测试与Internet Explorer我得到的结果:

Testing with Internet Explorer I get the results:

  • 如果我从我的本地作品消费服务
  • 如果我使用来自同一箱的服务(其中宁静的 托管)的作品
  • 如果我从withouth的SSL在另一台服务器消耗的服务
  • 但是,当我从HTTPS服务器消耗的服务不工作 IE浏览器。
  • If I consume the services from my local works
  • If I consume the services from the same box (where the restful are hosted) works
  • If I consume the services from another server withouth ssl works
  • But when I consume the services from a server on https dont work on IE.

所有先试工作,Chrome和FF的

All of the prior test work with Chrome and FF

这是即时通讯使用,使AJAX调用JavaScript

This is the javascript that im using to make the ajax call

function load(_url, _callback, _params, _type, _htmlMethod, _onStartFunction, _onFinishFunction) {
    var xhr;
    if (isFunction(_onStartFunction)) { _onStartFunction(); }
    ieVersion = getInternetExplorerVersion();
    if (typeof XMLHttpRequest !== 'undefined') {
        console.log("XMLHttpRequest");
        xhr = new XMLHttpRequest();
    }else {
        if (ieVersion > 7){ 
            console.log("XDomainRequest");
            xhr = new XDomainRequest();
        }else{
            var versions = [ "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp" ];
            for ( var i = 0, len = versions.length; i < len; i++) {
                try { 
                    console.log("ActiveXObject: " + versions[i]);
                    xhr = new ActiveXObject(versions[i]); 
                    break;
                } catch (e) { /* attempt next one*/ }
            }
        }
    }
    xhr.onreadystatechange = ensureReadiness;
    if (_type == JSON_TYPE) {
        contentType = "application/json";
    }else{
        contentType = 'text/plain';
    }

    function ensureReadiness() {
        if (xhr.readyState < 4) { return; }
        if (xhr.status !== 200) {
            showServiceDown();
            if (isFunction(_onFinishFunction)) { _onFinishFunction();}
            return;
        }
        if (xhr.readyState === 4) {
            if (isFunction(_onFinishFunction)) {_onFinishFunction(); }
            var responseText = "";
            responseText = xhr.responseText;
            if (_type == JSON_TYPE) {
                _callback(responseText);
            } else if (_type = HTML_TYPE) {
                var replaced = responseText.replace(/\\/g, '///');
                _callback(replaced);
            } else { _callback(responseText); }
        }
    }
    if ((_htmlMethod == undefined) || (_htmlMethod == null) || (_htmlMethod == "")) {
        _htmlMethod = METHOD_POST;
    }
    xhr.open(_htmlMethod, _url, true);
    xhr.withCredentials = true;
    xhr.setRequestHeader("Content-type", contentType);
    _params = (_params != undefined) ? _params : null;
    xhr.send(_params);
}

我不能使用任何JavaScript框架,为这个项目。

I can't use any javascript framework for this project.

该证书是MEDE内部由该公司是国内唯一的用途,所以任何浏览器验证邮件失败了,我不知道这有什么做的问题。

the certificates are mede internal by the company are for internal only usage so any browser validation mail fail, I dont know if this have something to do with the issue.

我希望任何人对这个问题的一些解决方案。

I hope anyone have some solution for this problem.

非常感谢您的时间。

推荐答案

我已经被解决加上空了类似的问题 onprogress 回调至XDomainRequest在IE的情况:

I've had a similar problem that was solved by adding a dummy onprogress callback to the XDomainRequest in the IE case:

if (ieVersion > 7){ 
    console.log("XDomainRequest");
    xhr = new XDomainRequest();
    xhr.onprogress = function() {}; // <-- add this
}

看来,IE浏览器被中止跨域请求,如果没有 onprogess 处理程序中定义。


下面是我使用AJAX功能,也许有东西在里面,帮助你:


Here's a function I use for AJAX, maybe there is something in it that helps you:

  /**
   * Wraps jQuery's AJAX, adds X-Domain support for IE
   * 
   *
   */
  function xDomainAJAX (url, settings) {
    jQueryRequired();
    $.support.cors = true; // enable x-domain
    if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && XDomainRequest) {
      // use ms xdr
      var xdr = new XDomainRequest();
      xdr.open(settings.type, url + '?' + $.param(settings.data));
      xdr.onprogress = function() {};
      xdr.onload = function() {
        settings.success(xdr.responseText);
      };
      xdr.onerror = settings.error;
      xdr.send();
    } else {
      // use jQuery ajax
      $.ajax(url, settings);
    }
  }

这篇关于CORS与IE浏览器,XMLHtt prequest和SSL(HTTPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆