Monday, November 5, 2012

Origin null is not allowed by Access-Control-Allow-Origin.

problem: when  i get jquery load html source error

XMLHttpRequest cannot load file:///D:/work/svn/pkrssweb/code/branches/20121105other1/third3/andriod/assets/www/plugin/rp_mst.htm. Origin null is not allowed by Access-Control-Allow-Origin.

 


fix 1:


from here: http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file

chrome.exe --allow-file-access-from-files

This may be acceptable for development environments, but little else. You certainly don't want this on all the time. This still appears to be an open issue (as of Jan 2011).

 

fix2:

from here: http://stackoverflow.com/questions/4742467/circumventing-chrome-access-control-allow-origin-on-the-local-file-system 

load js:

//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.
}
have bug in load html:
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);
}





other says:

me again. ok, it does work! have to do this: the source file puts the whole html into a javascript variable, var mytext="<html><body> etc etc". Fiddly, you have to make sure there are no " inside the html, and it all has to be on just one line. But if you do that, then all you need to do is say document.getElementById('mydiv').innerHTML=mytext and it all works. In Chrome, Mozilla. don't know about IE. but it should work ok.

No comments: