IE XdomainRequest CORS问题

时间:2013-09-04 22:20:23

标签: jquery internet-explorer cors xdomainrequest

我遇到了IE的问题(只有IE8 +,Chrome工作正常),当我尝试将信息发布到我网站上的其他页面时,我收到一条错误消息,说“在Access中找不到原点http://localhost:7230” -Control-Allow-Origin标题“。我知道这与CORS在某种程度上有关,但我不会超出我的领域。

发送请求的页面为:http://localhost:7230/TestPage.aspx

我要发布到http://localhost:7230/ActionHandler.aspx

的页面

发布到页面的代码:

function RequestData()
   {
      //If we have no data don't request anything, just reset the timer
      if (dataStore.topReadings.length == 0 && dataStore.specifiedRanges.length == 0 && dataStore.entireRanges.length == 0 && dataStore.periodRanges.length == 0)
      {
         setInterval(RequestData, options.interval);
      }

      var params = "?Action=GET_DATA";
      var body = GetRequestXML();

      var xmlhttp;

      if (window.XDomainRequest) // code for IE8 and IE9
      {
         xmlhttp = new XDomainRequest();
         if (xmlhttp)
         {
            xmlhttp.onerror = function ()
            {
               alert("[Data Config]Failed to send request for configuration!\n" + xmlhttp.responseText);
            };
            xmlhttp.ontimeout = function ()
            {
               alert('xdr ontimeout');
            };
            xmlhttp.onprogress = function ()
            {
            };
            xmlhttp.onload = function ()
            {
               if (xmlhttp.responseText)
               {
                  HandleResponseData($($.parseXML(xmlhttp.responseText)));
               }
            };

         } else
         {
            alert('failed to create xdr');
         }
      }
      else
      {
         if (window.XMLHttpRequest) // code for IE7, Firefox, Chrome, Opera, Safari
         {
            try
            {
               xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
               alert("[Data Request]Failed to create XMLHTTPRequest!\n" + e.message);
            }
         }
         else       // code for IE6, IE5
         {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }

         xmlhttp.onreadystatechange = function ()
         {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
               //alert("Handled!");
               HandleResponseData($($.parseXML(xmlhttp.responseText)));
               that.trigger("dataReceived");
            }
         }
      }

      try
      {
         xmlhttp.timeout = options.timeout;
         xmlhttp.open("POST", "http://localhost:7230/ActionHandler.aspx" + params, true);
      }
      catch (e)
      {
         alert("[Data Request]Failed to open XMLHTTPRequest!\n" + e.message);
      }

      setTimeout(function () { xmlhttp.send(body); }, 0);
   }

这是在visual studio中运行的ASP.NET网站。我已按照here步骤操作,并将相关行添加到我的web.config文件中。有关如何将我的请求传递到ActionHandler页面的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

通常情况下,如果您从一个域发布到另一个域,或者您从http来源发布到https终点,即使在相同的域名。

您是否尝试过设置此标题?

Access-Control-Allow-Origin", "*"

显然使用*有点过于宽泛,你想要缩小范围,但看看是否能解决问题。

相关问题