-1

Ok this is just crazy.

A friend of mine is testing out a JavaScript/jQuery stock widget for me. The content of the HTML file he used to test it couldn't be much more simple. It's these two lines:

<script src="http://quandl.io/widgets/js/q.js"></script>
<div name="quandl" data-values="msft revenue"></div>

For him this throws the error "[Error] XMLHttpRequest cannot load http://quandl.io/widgets/data/us-fundamentals.php?ticker=MSFT&indicator=REVENUE. Origin null is not allowed by Access-Control-Allow-Origin."

He sent me his file, and I get the same error. However, oddly in TextWrangler (a plain text editing app), the code in his file appears like this:

enter image description here

However, if I take that code and paste it in a new TextWrangler doc the code looks like this:

enter image description here

And get this: If I save that new file with the exact same code as the other file, the script executes with no problem. Both of these files are being executed from the same place (my local machine). Both, according to the properties I can inspect, are plain HTML files (no weird styling information I can see). It's odd, to say the least.

EDIT 1: Both files are on my local computer which is running Safari, OSX. Though I know this is not 100% kosher for StackOverflow, this problem is likely file-specific, so I'm posting here a link to the files. (But from what I can tell they are both vanilla text files with .html extensions and the same content... which is why this is so confusing)

Brian Risk
  • 1,244
  • 13
  • 23
  • Are you using the same browser in both cases? Some browsers allow XHR from `file://` URLs, others (like Chrome) do not. – T.J. Crowder Oct 30 '15 at 16:33
  • @T.J.Crowder: Yeah. Same everything. – Brian Risk Oct 30 '15 at 16:36
  • That specific Ajax error usually occurs when a script is trying to do ajax calls from a page that was loaded via a `file://` URL instead of via a web server and an `http://` URL. – jfriend00 Oct 30 '15 at 16:37
  • Well, as I'm sure you realize, what you describe is...extremely unlikely. So there's a difference **somewhere**. What browser? Can you create an [MCVE](/help/mcve)? – T.J. Crowder Oct 30 '15 at 16:37
  • @jfriend00: Which is what he said he's doing. The question would be why the results would be *inconsistent*. It should always work (some browsers) or always fail (Chrome, amongst others). – T.J. Crowder Oct 30 '15 at 16:38
  • @T.J.Crowder - he doesn't say anything about how he's loading the HTML file and we know exactly what causes that particular error message in some browsers so I'm explaining that. This is clearly a case of something that the OP is not aware of is different than described. We have to try to get them to see what that might be. – jfriend00 Oct 30 '15 at 16:40
  • @jfriend00: Yeah, I was just realizing that I'd interperted "local machine" to mean `file://`, but that's not necessarily the case. – T.J. Crowder Oct 30 '15 at 16:41
  • it's from file:// in the same browser. I've uploaded the two files here: https://www.dropbox.com/sh/8yozbgfo1v6p7ep/AACXJnoMFXJdJXHJDs39X_5Qa?dl=0 – Brian Risk Oct 30 '15 at 16:44
  • @BrianRisk: The question cannot rely on off-site content. Links rot, and people shouldn't have to follow some random link to help you. Post the content of the question **in** the question. – T.J. Crowder Oct 30 '15 at 16:45
  • @T.J.Crowder: I'm totally with you on that. Can I attach files to a question? This question seems very file-specific. – Brian Risk Oct 30 '15 at 16:47
  • @BrianRisk: No, just paste them in and mark them as code (Ctrl+K or the `{}` button). Even when people are asking questions about invisible space characters and such, that works very well. – T.J. Crowder Oct 30 '15 at 16:48
  • Again: What browser? – T.J. Crowder Oct 30 '15 at 16:49
  • @T.J.Crowder: I get what you're saying, but the point is that both files have the exact same code. – Brian Risk Oct 30 '15 at 16:49
  • Then what you're describing is clearly impossible. Thus, they do not have the same code. – T.J. Crowder Oct 30 '15 at 16:50
  • @T.J.Crowder: Sorry, it's Safari OSX. – Brian Risk Oct 30 '15 at 16:50
  • For reference: [Origin null is not allowed by Access-Control-Allow-Origin](http://stackoverflow.com/questions/8456538/origin-null-is-not-allowed-by-access-control-allow-origin) – jfriend00 Oct 30 '15 at 17:07

1 Answers1

1

I suspect that although you're loading the file from your "local machine" in both cases, in the one case you're doing it by double-clicking the file in your file explorer and getting a file:// URL, and in the other case you're doing it by opening the file from a locally-running web server process via a http:// or https:// URL.

It matters, because many browsers (such as Chrome) disallow all XMLHttpRequest calls from pages loaded over file:// URLs, on the basis that all origins are "cross-origin" and so denied by the Same Origin Policy. So if you load the file from a file:// URL, XHR will fail, but if you load it via http:// or https://, it may work.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I guess I'll delete this in a bit, since you've said clearly in the comments on the question that you're using `file://` in both cases. I find it **extremely** hard to believe it ever works, if you're using the same browser (and you've said you are) and getting that error in the one case. – T.J. Crowder Oct 30 '15 at 16:50