javascript - Ajax request rejected due to Origin 'null', origin IS the same and NOT on localhost or local file -
solved?, almost..
this related how chrome (47.0.2526.73) handles xml files. don't know details, code works fine in firefox (43.0.4).
i'm still curious why is, or how work in chrome.
what i'm trying do:
create javascript bookmarklet check sitemap xml links 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 on other page in domain, response is:
200
if navigate actual sitemap, http://www.example.com/sitemap.xml, same code responds with:
no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access.
since goal provide bookmarklet can invoked on sitemap itself, puts kink in plan.
how test this:
1) find xml website. google "filetype:xml sitemap" , response xml file (you'll find redirect you).
2) put code above in bookmarklet, or directly in developer's console of browser.
3) make sure variable sitemap set current url. compliant cors. sitemap=location.href;
what you'll find works fine in firefox, not in chrome.
note:
executing code html page, targeting html page does work.
executing code html page, targeting xml page does work.
executing code xml page, targeting html page does not work.
executing code xml page, targeting xml page does not work.
research i've done:
everything can find on error (understandably) related to:
- cross domain requests
- having either source or target on localhost, file:///, or otherwise on local machine.
my scenario neither of these.
ok.
before, said:
so when view xml file firefox or chrome (or ie, presumably), viewing a document created browser's inbuilt xml parser.
in case of chrome, served (nodomain), , identified so:
/* copyright 2014 chromium authors. rights reserved. * use of source code governed bsd-style license can * found in license file. */
so though url says "http://www.example.com/sitemap.xml", , in console window.location.href "http://www.example.com/sitemap.xml", and location.origin ""http://www.example.com", in actuality, origin (nodomain) if extension page. because is.
so origin null xml pages.
this not case.
i found this:
chrome adding origin header same-origin request
testing on firefox confirms ff not set origin on same-origin or head requests, chrome does. not problem, on xml pages document.domain set null. therefore origin sets null.
possibly bug in chrome? or intentional?
i'm still not satisfied own answer...
test this:
- go xml page in chrome.
- in console, make ajax request.
check request headers in network tab:
accept:*/* accept-encoding:gzip, deflate, sdch accept-language:en-gb,en-us;q=0.8,en;q=0.6 cache-control:no-cache connection:keep-alive host:www.example.com origin:null pragma:no-cache user-agent:mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/47.0.2526.73 safari/537.36
go upvote jaromanda-x intuition led me answers.
Comments
Post a Comment