SOLVED?, almost..
This is related to how Chrome (47.0.2526.73) handles xml files. I don't know the details, but this code works perfectly fine in Firefox (43.0.4).
I'm still curious as to why this is, or how to get it to work in Chrome.
What I'm trying to do:
Create a javascript bookmarklet to check sitemap xml links for 404s/500s/etc.
Code snippet in question:
var siteMap="http://www.example.com/sitemap.xml";
var httpPoke = function(url,callback){
var x;
x = new XMLHttpRequest();
x.open('HEAD', url);
x.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(this.status);
}
}
x.send();
};
var response=httpPoke(siteMap,function(n){
console.log(n);
});
If I am on any other page in the domain, response is:
200
If I navigate to the actual sitemap, http://www.example.com/sitemap.xml, the same code responds with:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Since my goal is to provide a bookmarklet that can be invoked on the sitemap itself, this puts a kink in my plan.
How to test this:
1) Find any xml from some website. Google "filetype:xml sitemap" and look for a response that is just an xml file (you'll find some will redirect you).
2) Put the code above in a bookmarklet, or directly in the developer's console of your browser.
3) Make sure variable siteMap is set to the current URL. This is to be compliant with CORS. You could even do siteMap=location.href;
What you'll find is that it works fine in Firefox, but not in Chrome.
Note:
Executing code FROM an HTML page, targeting an HTML page does work.
Executing code FROM an HTML page, targeting an XML page does work.
Executing code FROM an XML page, targeting an HTML page does not work.
Executing code FROM an XML page, targeting an XML page does not work.
Research I've done:
Everything I can find on this error is (understandably) related to:
- Cross domain requests
- Having either the source or target on localhost, file:///, or otherwise on your local machine.
My scenario is neither of these.