Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 异步填充Sammy.Storage缓存_Ajax_Jquery_Sammy.js - Fatal编程技术网

Ajax 异步填充Sammy.Storage缓存

Ajax 异步填充Sammy.Storage缓存,ajax,jquery,sammy.js,Ajax,Jquery,Sammy.js,在成功回调之外的jQuery$.ajax对象上访问requestJSON时遇到困难。如果我这样做: var ajax_request = $.ajax({ dataType: 'json', contentType: 'application/json' }); console.log(ajax_request.responseJSON); // this results in `undefined` 如何在不添加.success回调的情况下访问responseJSON?如

在成功回调之外的jQuery$.ajax对象上访问requestJSON时遇到困难。如果我这样做:

var ajax_request = $.ajax({
    dataType: 'json',
    contentType: 'application/json'
});

console.log(ajax_request.responseJSON);

// this results in `undefined`
如何在不添加.success回调的情况下访问responseJSON?如果我在Firebug中检查ajax_请求,我可以看到responseJSON属性和我期望的数据,但我无法通过以下方式访问它:

ajax_request.responseJSON
更具体地说,我正在用Sammy和Knockout建立一个水疗中心。在某些路由中,我需要能够从缓存中获取JSON,如果它不存在,则从服务调用中获取值,然后将其设置到缓存中:

var cached_json = storage.fetch('cached_json', function() {
    // make service call
    return $.getJSON(url);
});

event_context.render('template.tpl', {'json': cached_json}).appendTo('#my-target');

但是,当然,调用storage.fetch不会导致代码的其余部分暂停,直到完成$.getJSON。这是我不太清楚如何构造的部分。

以下是我将如何实现它

responseJSON = "";
$.get("myurl.php",function(jdata){
        responseJSON = jdata;
},"json");
我希望看到ajax方法,但在您的情况下,您也可以通过

....
success : function(jdata){ responseJSON = jdata; }
....

PS:我认为初始化空白响应子是不需要的,因为没有var的任何变量都在全局范围内,但这有助于明晰< /P> < P>我通过创建一个延迟对象来解决这个问题,它得到或创建了我需要的值:

function get_or_create_cache(storage, key, service_endpoint) {
    return $.Deferred(function(deferred) {
        var c = storage.get(key);
        if (c === null || c === undefined) {
            $.when(jsonp_service_request(service_endpoint)).done(function(json) {
                storage.set(key, json);
                deferred.resolve(json);
            });
        }
        else {
            deferred.resolve(c);
        }
    }).promise();
}
在这个函数中,存储是指Sammy.storage实例。jsonp_service_request是一个本地函数,它返回一个jsonp响应,并考虑本地开发的location.hostname,在这里我指向local.json文件,或者在远程环境中我调用实际的API。jsonp_服务_请求返回$.ajax函数

然后在我的Sammy路线中,我可以做:

this.get('#/', function(event_context) {
    $.when(get_or_create_cache(storage, 'my-cache-key', 'service-endpoint'))
        .then(function(json) {
            event_context.render('my-template.template', {'value-name': json})
                .appendTo('#my-target');
        });
});

$.GetJSONRequestParams$.getJSON的可能副本默认情况下是异步的,在某些情况下,我需要进行同步调用。我没有看到.getJSON的配置选项使其同步…如果不使用回调函数,您无法以任何方式访问外部响应。。。。有没有什么理由不使用回调..我对我的用例添加了更详细的解释。我想我在重新构造代码时遇到了问题,因此模板呈现只在值从缓存中出现时发生,但是填充缓存的调用是异步的。