简体   繁体   English

Angularjs $ http从IE9中的本地磁盘问题加载数据

[英]Angularjs $http load data from local disk issue in IE9

Angularjs, angular-1.2.0-rc.2 Angularjs,角度为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");
        });

This code runs perfectly fine when load the page from server, or when run the page on local disk using firefox or chrome. 从服务器加载页面时,或使用firefox或chrome在本地磁盘上运行页面时,此代码运行完全正常。 But when load the page from disk in IE9 (didn't test in other IE version), it always go to error function. 但是当在IE9中从磁盘加载页面时(没有在其他IE版本中测试),它总是转到错误功能。 When debugging, when error happen, the data, status, headers all are undefined, except config has content. 在调试时,当发生错误时,数据,状态,标题都是未定义的,除了config有内容。

Anyone have any idea? 任何人都有任何想法? Could be any solution? 可以是任何解决方案?

it is due to same origin policy You would see same error in chrome. 这是由于相同的原始政策你会在chrome中看到相同的错误。 Chrome and IE does not allow local ajax requests. Chrome和IE不允许本地ajax请求。 there are ways to do that for example chrome you would have to start it with some special flags 有办法做到这一点,例如chrome你必须用一些特殊标志启动它

http://joshuamcginnis.com/2011/02/28/how-to-disable-same-origin-policy-in-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

and in ie you can reduce your security settings to be minimal. 并且你可以将安全设置降低到最低限度。

there are other solutions like jQuery $.ajax run as a local HTML file avoiding SOP (same origin policy) 还有其他解决方案,如jQuery $ .ajax作为本地HTML文件运行,避免SOP(相同的原始策略)

use jquery's 使用jquery的

$.support.cors = true;

Another Solution 另一种方案
try to put the json data in js file or your server that way you don't have to do any ajax request to local file 尝试将json数据放入js文件或您的服务器,这样您就不必对本地文件执行任何ajax请求

Other sources: 其他来源:
Ways to circumvent the same-origin policy 如何规避同源政策
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/ http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM