【问题标题】:Angularjs $http load data from local disk issue in IE9Angularjs $http 从 IE9 中的本地磁盘问题加载数据
【发布时间】:2013-09-07 14:48:01
【问题描述】:

Angularjs,angular-1.2.0-rc.2

$http.get("gridData.json")
        .success(function(data, status, headers, config){
            angular.extend($scope.model.entities, data.entities);
        })
        .error(function(data, status, headers, config){
            alert("x");
        });

当从服务器加载页面时,或者使用 firefox 或 chrome 在本地磁盘上运行页面时,此代码运行良好。但是在 IE9 中从磁盘加载页面时(在其他 IE 版本中未测试),它总是进入错误功能。调试时,发生错误时,数据,状态,标题都是未定义的,除了配置有内容。

有人知道吗?有什么解决办法吗?

【问题讨论】:

    标签: javascript internet-explorer angularjs


    【解决方案1】:

    这是由于same origin policy 你会在 chrome 中看到同样的错误。 Chrome 和 IE 不允许本地 ajax 请求。有一些方法可以做到这一点,例如 chrome 你必须用一些特殊的标志来启动它

    http://joshuamcginnis.com/2011/02/28/how-to-disable-same-origin-policy-in-chrome/

    C:\Users\YOUR_USER\AppData\Local\Google\Chrome\Application\chrome.exe --allow-file-access-from-files --disable-web-security
    

    在 ie 中,您可以将安全设置降至最低。

    还有其他解决方案 喜欢 jQuery $.ajax run as a local HTML file avoiding SOP (same origin policy)

    使用jquery的

    $.support.cors = true;
    

    另一种解决方案
    尝试将 json 数据放在 js 文件或您的服务器中,这样您就不必对本地文件执行任何 ajax 请求

    其他来源:
    Ways to circumvent the same-origin policy
    http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2017-03-05
      • 2021-07-13
      • 2022-06-27
      • 2013-06-10
      • 2012-10-12
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多