简体   繁体   English

如果localStorage中存在数据,如何将其返回,否则执行AJAX请求?

[英]if data exists in localStorage, how to return it, or otherwise perform AJAX request?

Sorry for poor wording. 对不起,措辞不好。 Here's the code 这是代码

getJSON : function(url) {

    if ( localStorage.getItem(url) ) {

      console.log('Getting from localStorage instead');

      return $.parseJSON(localStorage.getItem(url));

    } else {
      console.log('No results locally, making XHR request...');

      return $.ajax({
        url: 'curl.php',
        type: 'GET',
        data: {
          url   : url
        }
      }).done(function (result) {
        var json = $.parseJSON(result);

        console.log('Setting to localStorage with key:', url);
        localStorage.setItem(url, result);
      })
    }
}

Two ways of getting the data: 两种获取数据的方式:

If it's in localStorage, this works: 如果在localStorage中,则可以使用:

console.log(this.getJSON(url))

If it's not and it's returning the AJAX object: 如果不是,它将返回AJAX对象:

this.getJSON(url).done(function (result) {
  console.log(result);
})

How can this be reformatted so that I can get the data without anticipating of if it's in localStorage or not? 如何重新格式化它,以便我可以在不预料是否在localStorage中的情况下获取数据?

You could add a callback parameter to your getJSON method like so: 您可以将回调参数添加到getJSON方法中,如下所示:

this.getJSON = function(callback)
{

if ( localStorage.getItem(url) ) {

  console.log('Getting from localStorage instead');
  // If it's in local storage execute callback immediately
  callback($.parseJSON(localStorage.getItem(url)));

} else {
  console.log('No results locally, making XHR request...');

  $.ajax({
    url: 'curl.php',
    type: 'GET',
    data: {
      url   : url
    }
  }).done(function (result) {
    var json = $.parseJSON(result);

    console.log('Setting to localStorage with key:', url);
    localStorage.setItem(url, result);
    callback(json);
  });
}

}

You could then call 然后你可以打电话

getJSON(function(json) {
  // Do something with json
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用cakePHP执行ajax请求? - how to perform an ajax request with cakePHP? 如果启用了javascript,如何执行Ajax请求,否则,请求将通过POSTBACK执行? - how to execute Ajax request if javascript is enabled, otherwise, request will be by POSTBACK? 如果存在选定的新数据,如何从localstorage更新数据 - How to update data from localstorage if new data selected exists javascript ajax请求中的if ... else语句如何执行? - How to perform if…else statement in javascript ajax request? 如果可用,如何创建角度服务以从 localstorage 获取数据,否则调用 api 方法 - How to create angular service to get data from localstorage if available otherwise call api method 如何从 JavaScript ajax 调用和打印来自 Django 模板中视图的返回数据 - How can I send localstorage data from JavaScript ajax call and print return data from views in Django template 如果ajax调用,则返回json,否则返回html - return json if ajax call otherwise return html 如何返回从jQuery Ajax请求提交的数据? - How can I return the data submitted from a jQuery Ajax request? 如何在javascript中成功的异步ajax请求返回JSON数据 - How to return JSON data on successfull async ajax request in javascript Lodash 如果存在则返回键,否则返回另一个键 - Lodash return key if exists, otherwise return the other key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM