jQuery延迟-传递$的结果。当美元区间

时间:2022-08-23 11:07:42

I'm starting to use jQuery Deferred objects a bit more and I run into this issue:

我开始更多地使用jQuery延迟对象,我遇到了这个问题:

I have a central AJAX function that performs pre validation of data, ajax set up and a few other things that sends to the server and returns xyz as data. How do I access 'xyz' in the 'then' part of a $.when(ajaxfn).then(dosomethingwithresult()); I get that ajaxfn returns a deferred object, but is there any way to pass the xhr's responseText forward?

我有一个*AJAX函数,它执行数据的预验证、AJAX设置和其他一些发送到服务器并返回xyz作为数据的东西。当(ajaxfn).然后(dosomethingwithresult());我得到ajaxfn返回一个递延对象,但是是否有办法通过xhr的responseText向前?

I'm essentially doing it like this...

我基本上是这样做的……

function ajaxfn(data) {
prevalidate(data);
return $.ajax(settings);
}
$.when(ajaxfn).then(function() {
// put 'xyz' on the page somewhere.
});

At the moment I'm just passing in a $.data pointer to the ajaxfn, and then in the success of the ajax request, I have $('body',pos,result) and then access it like that from inside then $('body).data(pos) == xyz. I'd like to know if there's a better way of doing what I've described?

现在我只交了一块钱。数据指针指向ajaxfn,然后在ajax请求成功时,我有$('body',pos,result),然后从内部访问它,然后是$('body).data(pos) = xyz。我想知道是否有更好的方法来做我所描述的事情?

2 个解决方案

#1


1  

Try this

试试这个

$.when(ajaxfn()).then(function(data) {
    // put 'xyz' on the page somewhere.
});

#2


2  

This can be simplified like so:

可以这样简化:

function ajaxfn(data) {
    prevalidate(data);
    return $.ajax(settings);
}

ajaxfn().then(function(response) {
    console.debug(response);
});

There is no need to use a $.when object. The ajax is already returning a promise.

没有必要使用$。当对象。ajax已经在回报一个承诺。

#1


1  

Try this

试试这个

$.when(ajaxfn()).then(function(data) {
    // put 'xyz' on the page somewhere.
});

#2


2  

This can be simplified like so:

可以这样简化:

function ajaxfn(data) {
    prevalidate(data);
    return $.ajax(settings);
}

ajaxfn().then(function(response) {
    console.debug(response);
});

There is no need to use a $.when object. The ajax is already returning a promise.

没有必要使用$。当对象。ajax已经在回报一个承诺。