0

I am trying to do display pages built client-side (HTML/Javascript) using JSON data.

  • 4 pages, built using the same JSON object So I don't want to retrieve the JSON each time I generate the page.

That is why I created this function:

function retrieve_user_info() {

////////
// Here, code that decides whether we use the local data or if we call the server again
////////

var request_in = {
    UserId:1
};

$.ajax({
    url: "http://localhost:2000/getInfo",
    async: false,
    cache: false,
    data: request_in,
    dataType: 'json',
    timeout: 4000,
    success: function(data) {
        console.log('success')
        returnValue = data;
    }
});

sessionStorage.user = returnValue;

}

In each page, I do the following: - call function retrieve_user_info(), to update (or not) the local copy - generate the page using the local copy

So this works locally using "async: false", but in cross-domain it fails, as written in the documentation. My question is how to handle this. - I do not want to duplicate the server call to each page. - I do want to avoid retrieving the data too often

Thanks in advance, S.

Edit: After what I can read, it seems that it is simply not possible. I don't think this is a duplicate, because I don't want to tie the function that retrieves info from server with the function that generates the page. But apparently I have to. Thank you for your answers.

Comments: Strange that there is no easy way to cache data locally

Omar
  • 32,302
  • 9
  • 69
  • 112
user992157
  • 161
  • 3
  • 8
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – elclanrs Jul 01 '13 at 21:19
  • In "cross domain" as you're trying to use ajax with an URL that is not to your own server etc. If so it's the same origin policy. And you generally should never ever ever ever need to do synchronous calls. – adeneo Jul 01 '13 at 21:21
  • I didn't see the `async: false` so technically not a dup, but I agree with Adaneo, don't do it... Take a look at the "dup" for more info on how to re-organize your code for AJAX. – elclanrs Jul 01 '13 at 21:22

0 Answers0