9

In my small JS web-application, I use some JSON data.

The data is on server in a separate static .json file.

My application, being small, does not use any frameworks, not even jQuery. And I do not want to mess with XMLHTTPRequest myself.

Is there a way to load my JSON data without AJAX? (And without renaming the file to .js and imitating JSONP or including the data in existing JS sources.)

It is OK if it will work only in modern browsers.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
  • 1
    You could simply include the data in your Javascript file. – Lars Kotthoff Sep 28 '13 at 21:36
  • 1
    @Lars, it could be 10 mb size – davidkonrad Sep 28 '13 at 21:37
  • @LarsKotthoff Don't want to either :) Updated the question. – Alexander Gladysh Sep 28 '13 at 21:38
  • 1
    It seems less than sensible to declare a web application 'too small to use jQuery' when it will be processing a potentially 10mb JSON object. – Tetsujin no Oni Sep 28 '13 at 21:40
  • My JSON data is a few hundreds of lines long. But I do not want to put it inside my source code anyway -- for aesthetical reasons :-) I'll better find myself some small and neat `XMLHTTPRequest` wrapper. – Alexander Gladysh Sep 28 '13 at 21:42
  • 1
    @Tetsujin, Yes - but there is really good reason in trying anything to avoid the inclusion of a 9789 lines library (not minified), if the same thing easily can be done in pure javascript. – davidkonrad Sep 28 '13 at 21:53
  • ...Is that a good one? https://github.com/berthojoris/http.js/blob/master/http.js – Alexander Gladysh Sep 28 '13 at 21:56
  • 2
    @davidkonrad: I am both with you on that, and at the same time consider jquery-current to be the equivalent of MSVCRT(current).dll as a dependency. Certainly not worried about 9kloc unminified when we're talking about js that's a 304 on a well-configured reference. – Tetsujin no Oni Sep 28 '13 at 21:56
  • 1
    @Tetsujin, I follow you, but I still think you not should be bullied / "tyrannisized" (dont know the exact english term) by the availability of "cheap" libraries you just include as it was cargo programming. If the goal is a site as minimal as possible - and I like that idea - when loadtime or CDN's doesnt matter. – davidkonrad Sep 28 '13 at 22:19
  • I think you're looking for 'seduced' for that usage, and there's definitely a case for the desirability of understanding the underpinnings of what you're doing. That said, there needs to be a value-carrying reason for that goal. I'd rather carry a certain weight of libraries and only write code that actually carries real value for my problem domain. My problem domains don't generally include "implement my own XHR functions". – Tetsujin no Oni Sep 29 '13 at 00:29

1 Answers1

7

There aren't any sensible ways.

You might be able to display it in an iframe and read it from there, but (since you will have to deal with timing issues) that is at least as complicated as using XMLHttpRequest, and risks browsers deciding that JSON should be downloaded instead of rendered.

Use XMLHttpRequest. It isn't complicated.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    While using `XMLHTTPRequest` is not complicated, it requires me to switch from `file://` to an actual HTTP server for my development workflow. Which is a little more cumbersome. But, thanks to `python -mSimpleHTTPServer`, not too much. – Alexander Gladysh Sep 29 '13 at 11:01