English 中文(简体)
多个被推迟的Ajax呼吁及其支持者的付费数据
原标题:Pairing Data from Multiple Deferred Ajax Calls with their Caller

我在多封推迟的Ajax电话中工作,我想对我如何获得数据充满活力。 我不想在几个参数中硬化,而是单独使用,而是想通过论点反对获取数据。 这项工程课以罚款,但Im无法从Ajax电话的json中确定数据。 我可以通过确定允诺对象(some)的ur,或确定Ajax电话所执行并假定数据与否。

在此,我迄今为止的法典(为了说明我想要做的事情而ock):

promises = [ 
    $.get("example.php", {}, function(){}),
    $.get("list.php", {}, function(){}),
    $.get("test.php", {}, function(){}),

]

$.when.apply(null, promises).then( function() {
     jsonarray = []
     $.each(arguments, function(index, value){
         // this is what I would like to work but doesn t
         // promises[index].success.url is how I imagine accessing 
         //"list.php" or "test.php"
         if (jsonarray[promises[index].success.url] == null){
              jsonarray[promises[index].success.url] = []
         }
         jsonarray[promises[index].success.url] = value
         doSomethingWith(jsonarray)
     })

是否有另一种办法将每一论点与Ajax呼吁加以匹配? 我不想做的是:

$.when.apply(null, promises).then( function(examplejson, listjson, testjson) {
         // this is lame
         exampledoSomethingWith(examplejson)
         listdoSomethingWith(listjson)
         testdoSomethingWith(testjson)
})

Thanks! Sara

最佳回答

rel=“nofollow”> http://jsfiddle.net/6EZsh/2/。

$(function() {

    var objs = [{ one :1},{ two :2},{ three :3}];
    var urls = [];

    ajaxCall = function(i) {
        return $.ajax({
            url: /echo/html/ ,
            method:  POST ,
            data: {id:i}
        }).done(function () {
            urls.push({obj:i,url:this.url});
        });   
    }

   $.when.apply($,
        objs.map(function(i) {
            return ajaxCall(i);
        })
    ).then(
        console.log(urls)
    );

});

正如你在你的“晚年”中所看到的,是你们所有被推迟者的成功处理者。 因此,弥补这一信息的唯一真正途径是你Ajax电话的成功传承者。 见上文例。

问题回答

让审判

var urls = [
    "example.php",
    "list.php",
    "test.php"
];
var promises = [  
];

var jsonarray = {};
$.each(urls, function(index, value) {
    promises[index] = $.get(urls[index], {}, function(){});
    promises[index].success(function(data) {
        jsonarray[urls[index]] = data; //is this the value you want in the jsonarray??
    });
});

$.when.apply(null, promises).then( function() {
    doSomethingWith(jsonarray) 
});




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签