English 中文(简体)
IE9 与SproutCore的交叉来源申请
原标题:Cross origin request with SproutCore in IE9

我正试图根据SproutCore 1.8建立一个网络。 为了从服务器中检索数据,该数据显示,COSPA系统向一个单独的领域运行的REST网络服务提出请求。

这部法典如下:

var request = SC.Request.getUrl( http://example.com/some/path );
request.set( attachIdentifyingHeaders , NO);    
request.json().notify(this, this.didFetchData).send();

这在“ Chrome”、“Sato”和“Little”中发挥了巨大作用,但在“因特网探索者9”中却做了大量工作。 事实上,IE9在SproutCore的内部请求执行中,变成了“Access Denied”的 Java本错误。 错误在这条线上提出:

// initiate request.
rawRequest.open(this.get( type ), this.get( address ), async);

在经过一些简短的调查之后,我发现,微软在IEE9中为索再解放联盟的请求实施了专门的XDomainRequest。 这似乎没有得到SproutCore的支持,因为我从这些路线(临时土著申请执行选择):

return tryThese(
  function() { return new XMLHttpRequest(); },
  function() { return new ActiveXObject( Msxml2.XMLHTTP ); },
  function() { return new ActiveXObject( Microsoft.XMLHTTP ); }
);

Is this a shortcoming of SproutCore or am I missing something? If so, do you have any suggestions as to how I could work around the issue without writing my own request abstraction?

请注意,COSPAS是我所投入的现有服务器基础设施的一项要求。 我既不能把服务放在与向客户提供服务的服务器相同的领域,也不能用反向法或类似的基础设施解决问题。

最佳回答

You have two options

(1) Hack Sprout分公司。 您可以尝试增加

<代码>(功能)(恢复新的XDomainRequest();>}

注 添加第一段;如果在浏览器上提供,则使用该文本,否则该代码将退回其他物体。

(2) 如果你不想修改SC来源,你可以自行创建。 App.CorsResponse 页: 1 在执行过程中,提供您自己的<代码>createRequest。 方法与做我在1年所说的话。 每当你提出要求时,就指定“<>responseClass为您的习惯执行。

问题回答

I have quickly coded a solution based on hvgotcodes suggestion. The code below implements a subclass of SC.XHRResponse that adds the required functionality for handling IE-specific XDomainRequest CORS requests. Note that this does not handle errors in IE when using XDomainRequest yet.

MyApp.CorsRequest = SC.XHRResponse.extend({
  createRequest: function() {
    function tryThese() {
      for (var i=0; i < arguments.length; i++) {
        try {
          var item = arguments[i]();
          return item;
        } catch (e) {}
      }
      return NO;
    }

    return tryThese(
      function() { return new XDomainRequest(); },
      function() { return new XMLHttpRequest(); },
      function() { return new ActiveXObject( Msxml2.XMLHTTP ); },
      function() { return new ActiveXObject( Microsoft.XMLHTTP ); }
    );
  },

  invokeTransport: function() {
    var rawRequest, transport, handleReadyStateChange, async, headers;

    rawRequest = this.createRequest();
    this.set( rawRequest , rawRequest);

    // configure async callback - differs per browser...
    async = !!this.getPath( request.isAsynchronous );

    if (async) {
      if (!SC.browser.isIE && !SC.browser.isOpera) {
        SC.Event.add(rawRequest,  readystatechange , this,
                     this.finishRequest, rawRequest);
      } else if(SC.browser.isIE) {
           transport = this;
        handleLoad = function() {
          if (!transport) { return null; }
          var ret = transport.finishRequest();
          if (ret) { transport = null; }
          return ret;
        };
        rawRequest.onload = handleLoad;          
      } else {
        transport = this;
        handleReadyStateChange = function() {
          if (!transport) { return null; }
          var ret = transport.finishRequest();
          if (ret) { transport = null; }
          return ret;
        };
        rawRequest.onreadystatechange = handleReadyStateChange;
      }
    }

    // initiate request.
    rawRequest.open(this.get( type ), this.get( address ), async);

    // now send the actual request body - for sync requests browser will
    // block here
    rawRequest.send(this.getPath( request.encodedBody )) ;
    if (!async) { this.finishRequest(); }

    return rawRequest;
  }, 

  finishRequest: function(evt) {
    var rawRequest = this.get( rawRequest ),
        readyState = rawRequest.readyState,
        error, status, msg;

       if (SC.browser.isIE) {
            readyState = 4;
            rawRequest.status = 200;
       }       

    if (readyState === 4 && !this.get( timedOut )) {
      this.receive(function(proceed) {
        if (!proceed) { return; }

        // collect the status and decide if we re in an error state or not
        status = -1;
        try {
          status = rawRequest.status || 0;
        } catch (e) {}

        // if there was an error - setup error and save it
        if ((status < 200) || (status >= 300)) {

          try {
            msg = rawRequest.statusText ||   ;
          } catch(e2) {
            msg =   ;
          }

          error = SC.$error(msg || "HTTP Request failed", "Request", status);
          error.set("errorValue", this) ;
          this.set( isError , YES);
          this.set( errorObject , error);
        }

        // set the status - this will trigger changes on related properties
        this.set( status , status);
      }, this);

      // Avoid memory leaks
      if (!SC.browser.isIE && !SC.browser.isOpera) {
        SC.Event.remove(rawRequest,  readystatechange , this, this.finishRequest);
      } else {
           if (window.XDomainRequest)
                rawRequest.onload = null;
           else
             rawRequest.onreadystatechange = null;
      }

      return YES;
    }
    return NO;
  } 
});




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签