简体   繁体   English

绕过本地文件系统上的Chrome Access-control-allow-origin?

[英]Circumventing Chrome Access-control-allow-origin on the local file system?

I've read the other same origin policy topics here on SO, but I haven't seen any solutions related to the local file system. 我已经在SO上阅读了其他相同的原始策略主题,但我还没有看到任何与本地文件系统相关的解决方案。

I have a web app (In a loose sense of the word) that must be local served. 我有一个网络应用程序(在一个松散的意义上)必须在本地服务。 I am trying to load a large amount of data in after the user has loaded the page, depending on what they are doing on the webpage. 我试图在用户加载页面后加载大量数据,具体取决于他们在网页上做了什么。 In Firefox 3.5 and IE8 I am able to use jQuery's AJAX() and GetScript() methods to do this, but in Chrome this fails due to the Same Origin Policy. 在Firefox 3.5和IE8中,我可以使用jQuery的AJAX()和GetScript()方法来执行此操作,但在Chrome中由于同源策略而失败。

XMLHttpRequest cannot load file://test/testdir/test.js . XMLHttpRequest无法加载file://test/testdir/test.js Origin null is not allowed by Access-Control-Allow-Origin . by Access-Control-Allow-Origin不允许by Access-Control-Allow-Origin null

This happens when I do something simple like 这种情况发生在我做一些简单的事情时

$.getScript("test.js");

This functions perfectly well in IE & Firefox. 这在IE和Firefox中运行良好。

After reading a bunch about this, I decided to try writing directly into the head of the document. 在阅读了大量有关此内容之后,我决定尝试直接写入文档的头部。 In the console in Chrome I typed the following: 在Chrome的控制台中,我键入了以下内容:

var head = document.getElementsByTagName("head")[0];  
var script =document.createElement('script');   
script.id = 'uploadScript';  
script.type = 'text/javascript';  
script.src = "upload.js";   
head.appendChild(script);

This works fine when pasted in the console- the <script...test.js</script> element is added to the head, evaluated, and content loaded into the DOM. 这在粘贴到<script...test.js</script>时工作正常 - 将<script...test.js</script>元素添加到头部,评估并将内容加载到DOM中。

I thought this was successful, until I put this code into a function call. 我认为这是成功的,直到我将此代码放入函数调用。 The same exact code, when called from a function, adds the element to the but does not evaluate the JavaScript file. 从函数调用时,相同的确切代码会将元素添加到但不会评估JavaScript文件。 I can not figure out why. 我无法弄清楚为什么。 If I use Chrome's console to stop execution in the method that it is adding the element to the and run the above code, it does not evaluate it. 如果我使用Chrome的控制台停止执行它正在添加元素的方法并运行上面的代码,它就不会对它进行评估。 However, if I unpause the execution and run the exact same code (pasting it in the console window) it works. 但是,如果我取消暂停执行并运行完全相同的代码(在控制台窗口中粘贴它),它就可以工作。 I'm at a loss to explain this. 我无法解释这一点。 Has anyone dealt with this before? 以前有人处理过这个吗?

I've read up on the following SO posts, but they are not describing the problem that I have: 我已经阅读了以下SO帖子,但他们没有描述我的问题:

Ways to circumvent the same-origin policy 如何规避同源政策
XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless) XMLHttpRequest Origin null不允许Access-Control-Allow-Origin for file:/// to file:///(Serverless)
Cross-site XMLHttpRequest 跨站点XMLHttpRequest

Again, my last resort is to load all the data at the webpage's load- This can cause up to a 10 second delay in loading the webpage that is unnecessary for 90% of the app's users. 同样,我最后的办法是在网页加载时加载所有数据 - 这可能导致加载网页最多延迟10秒,这对90%的应用用户来说是不必要的。

Thanks for any suggestions/alternatives!!! 感谢您的任何建议/选择!

I think I've figured it out. 我已经明白了。

All I really needed to do was add a callback into my <script> tag. 我真正需要做的就是在我的<script>标签中添加一个回调。 Final code: 最终代码:

I have an element named next... So, in the $("#next").click() function I have the following code. 我有一个名为next的元素...所以,在$("#next").click()函数我有以下代码。 This only gets executed if they click "next". 只有在点击“下一步”时才会执行此操作。

//remove old dynamically written script tag-    
       var old = document.getElementById('uploadScript');  
   if (old != null) {  
     old.parentNode.removeChild(old);  
     delete old;  
   } 

   var head = document.getElementsByTagName("head")[0];  
   script = document.createElement('script');  
   script.id = 'uploadScript';  
   script.type = 'text/javascript';  
   script.src = 'test/' + scope_dir + '/js/list.js';  
   script.onload = refresh_page;    
   head.appendChild(script);  


function refresh_page(){  
   //perform action with data loaded from the .js file.  
}  

This seems to work, and allows Chrome to dynamically load .js files on the local file system while circumventing the access-control-allow-origin policy I ran into while trying to use jQuery functions. 这似乎有效,并允许Chrome在本地文件系统上动态加载.js文件,同时避免在尝试使用jQuery函数时遇到的access-control-allow-origin策略。

Ok, done a lot of fiddling and wasted a lot of time. 好吧,做了很多摆弄,浪费了很多时间。 But I got it figured. 但我明白了。 The solution in my last answer works fine for Chrome, and for Mozilla. 我的上一个答案中的解决方案适用于Chrome和Mozilla。 But it does not work for blessed IE, because IE will not fire the onload event: it thinks it has dealt with all the onloads in this file and you can't get it to do another one. 但它不适用于受祝福的IE,因为IE不会触发onload事件:它认为它已经处理了这个文件中的所有onloads,你不能让它做另一个。 However, IE is quite happy to load the file up using the JQuery getScript function (which Chrome will not permit because of the ccess-control-allow-origin policy) -- you will need the JQuery libraries for this to work. 但是,IE非常乐意使用JQuery getScript函数加载文件(由于ccess-control-allow-origin策略,Chrome不允许这样做) - 您需要使用JQuery库才能使用它。 So here is what I ended up with: 所以这就是我最终的结果:

function getMyText(){
    var url='mylocalfile.js';
    if (jQuery.support.scriptEval) { 
        var old = document.getElementById('uploadScript');  
        if (old != null) {  
             old.parentNode.removeChild(old);  
             delete old;  
            } 
        var head = document.getElementsByTagName("head")[0]; 
        var script = document.createElement('script');
        script.id = 'uploadScript';
        script.type = 'text/javascript';
        script.onload = refresh_page; 
        script.src = url; 
        head.appendChild(script);  
    } else {
       $.getScript(url,function(){
            refresh_page();
      });
     }
}

function refresh_page() {
    alert(mytext);
}

In all this, mylocaltext.js defines all my html as the content of a variable, mytext. 在所有这些中,mylocaltext.js将我的所有html定义为变量mytext的内容。 Ugly, but it works. 丑陋,但它的工作原理。 jQuery.support.scriptEval is true for intelligent browsers that fire onload events if the DOM changes. 对于在DOM发生更改时触发onload事件的智能浏览器,jQuery.support.scriptEval为true。 IE does not, so that sends it to .getScript; IE没有,所以将它发送到.getScript; Chrome and others do, so that sends them the other way. Chrome和其他人这样做,因此以另一种方式发送它们。 Anyway this works on local files. 无论如何,这适用于本地文件。

Interesting, but how can I use the same technique to load in a whole HTML file? 有趣,但我如何使用相同的技术加载整个HTML文件? Similar problem to yours -- I have hundreds of HTML files which I want to include within a web page, depending on what the user wants to see. 你的类似问题 - 我有数百个HTML文件,我想要包含在一个网页中,具体取决于用户想要看到的内容。 It seems I can use this technique to load in a whole HTML page, something like: 似乎我可以使用这种技术加载整个HTML页面,如:

script.id = 'uploadScript';
script.type = 'text/html';
script.src = url; 
script.onload = refresh_page; 
head.appendChild(script);  

ie, tell it load in HTML. 即,告诉它在HTML中加载。 I can see from the console that it is loading it into the page, and I get a message 'Resource interpreted as script but transferred with MIME type text/html'. 我可以从控制台看到它正在将它加载到页面中,我收到一条消息“资源解释为脚本但是使用MIME类型text / html传输”。 But I cannot figure out any way to get at the HTML loaded in and held within the script 但我无法弄清楚是否有任何方法来获取加载并保存在脚本中的HTML

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

相关问题 Chrome中的错误:“ Access-Control-Allow-Origin不允许原始文件://” - Error in Chrome: “Origin file:// is not allowed by Access-Control-Allow-Origin” Chrome无法识别Access-Control-Allow-Origin - Access-Control-Allow-Origin is not recognized by Chrome DynamoDB本地访问控制允许来源 - DynamoDB Local Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用Chrome Origin null - Chrome Origin null is not allowed by Access-Control-Allow-Origin Chrome创建扩展以避免源文件// // access-control-allow-origin不允许 - Chrome create extention to avoid origin file // is not allowed by access-control-allow-origin WebWorker的Access-Control-Allow-Origin不允许使用原始file:// - Origin file:// is not allowed by Access-Control-Allow-Origin with WebWorker ExpressJS没有“ Access-Control-Allow-Origin”标头到本地服务器 - No 'Access-Control-Allow-Origin' header with ExpressJS to local server Access-Control-Allow-Origin未检查Chrome扩展名 - Access-Control-Allow-Origin not checking in chrome extension Ajax和本地网络给出“没有&#39;Access-Control-Allow-Origin&#39;标头” - Ajax And Local Network gives “No 'Access-Control-Allow-Origin' header” Node.js 文件以使用 access-control-allow-origin 运行本地服务器 - Node.js file to run a local server with access-control-allow-origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM