2

I have a web application whose entire functionality is dependent on 3 JSON files being fetched from the server and processed when the main page is loaded. At the moment, I use jQuery AJAX calls, with 'async:false', and it works fine. But everything I read is shouting at me that synchronous AJAX is evil and that I should refactor this to asynchronous behaviour. Of course I could do that, at the expense (as I see it) of making the code rather messier, in that the entire application would be loaded in the AJAX success function.

My question is whether I should really go ahead and refactor, given that this appears to me to be a possibly justified case for synchronous behaviour. Yes, the application will be unresponsive if there's a problem with the AJAX call, but it would also be unresponsive if there were a problem loading one of the key JS files, say, would it not? And given that the application is effectively unusable (can't do anything worthwhiile) until the JSON files are loaded anyway, does it matter?

Maybe I've missed a simple way of loading JSON files which doesn't involve the use of AJAX at all? That would be a cleaner solution, certainly.

John Moore
  • 6,739
  • 14
  • 51
  • 67
  • One question you should ask yourself is "will my site work if loading the JSON takes a long time?" – evolutionxbox Sep 12 '16 at 10:28
  • from " files being fetched from the server and processed when the main page is loaded" i can simply wander why can't you just load the json file with the page and need ajax? – madalinivascu Sep 12 '16 at 10:34

3 Answers3

3

If the files are located in your server, perhaps you can try this approach:

Define the JSON like this in a file:

data = '[
    {"name" : "Ashwin", "age" : "20"},
    {"name" : "Abhinandan", "age" : "20"}
]';

Then you just include the json file in your HTML.

<script type="text/javascript" src="data.json"></script>
<script type="text/javascript" src="javascrip.js"></script>

Finally, just use the data as a variable:

var mydata = JSON.parse(data);

Disclaimer: I just found this 10 minutes ago here

Community
  • 1
  • 1
Defesa Esquerdo
  • 290
  • 2
  • 3
  • 10
  • 1
    I had come across that link before but hadn't really given it serious consideration, because of the requirement that the JSON files should actually be in a format which wasn't JSON. On re-reading, though, I realise that this gives me the ideal solution, because I can generate the required JS file server-side, containing both the JSON data and the assignment to a variable. The JSON was being generated server-side anyway, so all I need to do is adapt it a little. – John Moore Sep 12 '16 at 10:58
  • anyone think this is a very wrong approach ? I mean bad programming style that would cause to regret in future ?I find it very much useful but is really worried about this method of approach – neo Sep 12 '16 at 11:51
  • In what way is it bad programming style? – John Moore Sep 12 '16 at 11:54
  • I wasn't raising an objection but was a doubt,not a hardcore programmer,but feels like little bit nonstandard,any experts here to give an advice ? – neo Sep 12 '16 at 12:53
  • 2
    This approach is perfectly valid and not in any way non-standard. There is a downside that it introduces the JSON data in the global scope but if you can live with that then there's no reason not to do this. – GordonM Sep 12 '16 at 13:21
1

Does it work? First and foremost don't "fix" what isn't broken.

As for whether you should use asynchronous mode, is this application of yours running on a local LAN or will it run over the public internet? In the former case it's probably safe to assume that you're unlikely to ever have bandwidth problems so there are few reasons to switch to asynch other than idealogical ones.

If it's running over the public internet, however, then there is potential for all kinds of network-related issues and you can't rely on the server/client connection staying good for the course of the session. In that case an asynchronous approach with a loading/activity screen of some sort is a must, along with suitably robust error handling/recovery and downmoding to a simpler version of the app in the event of an ideal connectivity state not being available, if at all possible

GordonM
  • 31,179
  • 15
  • 87
  • 129
0

I have adopted the approach mentioned in my comment in response to the suggestion from Defesa Esquerdo. It's working well and was a simple change. I kind of feel I've side-stepped my own question here, but it's a good solution to my problem - a simplification of the code which also improves efficiency.

John Moore
  • 6,739
  • 14
  • 51
  • 67
  • answers shift position based on active,oldest,votes.Please mention it in that answer comment section or mark accepted answer – neo Sep 12 '16 at 12:55