如何编写自定义回调成功方法来将响应存储在一个对象中而不是基于响应做一些事情?

时间:2022-04-12 19:54:03

For an ajax call like:

对于ajax调用,如:

$.ajax({
   ....
   success :function(data){

   },

   error :function(error){
});

Is there a way I can grab that data from success function and store it in a variable? Based on the data receive I have to store some property.

有没有办法可以从成功函数中获取数据并将其存储在变量中?基于数据接收我必须存储一些属性。

If not, can I do something like

如果没有,我可以做点什么

myCustomAjax({post data}, async, successFn, errorFn);

successFn would be the callback I want to manipulate from outside based on some behavior.

successFn将是我想根据某些行为从外部操纵的回调。

2 个解决方案

#1


1  

I still don't fully understand your question but the comment box isn't sufficient for what i need to type: This idea is really only relevant to an ajax call that receives JSON, a JSONP request could do some alternate things.

我仍然不完全理解你的问题,但评论框不足以满足我需要输入的内容:这个想法实际上只与接收JSON的ajax调用有关,JSONP请求可以做一些替代事情。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../scripts/jquery-1.5.1.min.js"></script>
</head>
<body>
    <script language="javascript">
        function doX() { alert("x"); }
        function doQ() { alert("q"); }
        function doY() { alert("y"); }
    (function () {
        $.ajax({
            url: "json.txt",
            dataType: 'json',
            cache: false,
            success: function (data, textStatus, jqXHR) {
                if (data.mySuccessProperty == "succeeded") {
                    doX();
                    (function () { eval(data.dynamicFunction)(); })();
                }
                else {
                    doY();
                    (function () { alert("Z"); })();
                }
            },
            error: function (jqXHR, textStatus, error) {
                // handle error
                alert("error" + error);
            }
        });
    })();

    </script>
</body>
</html>

json.txt contains

json.txt包含

{
 "mySuccessProperty": "succeeded",
 "dynamicFunction": "doQ"
}

the ouput will be

输出将是

alert x
alert q

Alot of this feels silly to me, I'm just coding random things you could do but they don't necessarily make sense without the context of an application using these ideas.

很多这对我来说很愚蠢,我只是编写你可以做的随机事情,但如果没有使用这些想法的应用程序的上下文,它们就不一定有意义。

#2


0  

var ajaxUrl = "/your/ajax/url";
myCustomAjax = function(data, isAsync, successCallback, errorCallback) {
    $.ajax(ajaxUrl, {
            data: data,
            async: isAsync,
            success: successCallback,
            error: errorCallback
    }
}

is this what you wanted?

这是你想要的吗?

and yes, you can store result in any variable you want, but my guess is that you don't really want to - you're dealing with asynchronous programming here, so you may bump into race conditions and other problems. use callbacks.

是的,你可以将结果存储在你想要的任何变量中,但我的猜测是你真的不想 - 你在这里处理异步编程,所以你可能遇到竞争条件和其他问题。使用回调。

#1


1  

I still don't fully understand your question but the comment box isn't sufficient for what i need to type: This idea is really only relevant to an ajax call that receives JSON, a JSONP request could do some alternate things.

我仍然不完全理解你的问题,但评论框不足以满足我需要输入的内容:这个想法实际上只与接收JSON的ajax调用有关,JSONP请求可以做一些替代事情。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../scripts/jquery-1.5.1.min.js"></script>
</head>
<body>
    <script language="javascript">
        function doX() { alert("x"); }
        function doQ() { alert("q"); }
        function doY() { alert("y"); }
    (function () {
        $.ajax({
            url: "json.txt",
            dataType: 'json',
            cache: false,
            success: function (data, textStatus, jqXHR) {
                if (data.mySuccessProperty == "succeeded") {
                    doX();
                    (function () { eval(data.dynamicFunction)(); })();
                }
                else {
                    doY();
                    (function () { alert("Z"); })();
                }
            },
            error: function (jqXHR, textStatus, error) {
                // handle error
                alert("error" + error);
            }
        });
    })();

    </script>
</body>
</html>

json.txt contains

json.txt包含

{
 "mySuccessProperty": "succeeded",
 "dynamicFunction": "doQ"
}

the ouput will be

输出将是

alert x
alert q

Alot of this feels silly to me, I'm just coding random things you could do but they don't necessarily make sense without the context of an application using these ideas.

很多这对我来说很愚蠢,我只是编写你可以做的随机事情,但如果没有使用这些想法的应用程序的上下文,它们就不一定有意义。

#2


0  

var ajaxUrl = "/your/ajax/url";
myCustomAjax = function(data, isAsync, successCallback, errorCallback) {
    $.ajax(ajaxUrl, {
            data: data,
            async: isAsync,
            success: successCallback,
            error: errorCallback
    }
}

is this what you wanted?

这是你想要的吗?

and yes, you can store result in any variable you want, but my guess is that you don't really want to - you're dealing with asynchronous programming here, so you may bump into race conditions and other problems. use callbacks.

是的,你可以将结果存储在你想要的任何变量中,但我的猜测是你真的不想 - 你在这里处理异步编程,所以你可能遇到竞争条件和其他问题。使用回调。