将数据移动到$.ajax's out

moving data to $.ajax's out?

本文关键字:out ajax 数据 移动      更新时间:2023-09-26

我想将"data"变量移到不成功函数中进行其他操作。

$("a[class=note]").click(function( evt ){
                    var note = $(this).attr("value");
                    var preid = $(this).attr("id");
                    $.ajax({
                        type: 'GET',
                        url: 'style/ajax.php',
                        data: 'do=note&value=' + note + '&preid=' + preid,
                        success: function(data)
                        {
                            alert(data);
                        }
                    });
                });

例如php有Global pharase。。

全局var(这是更糟糕的解决方案,但这正是您所要求的):

$("a.note").click(function( evt ){
                var note = $(this).attr("value");
                var preid = $(this).attr("id");
                $.ajax({
                    type: 'GET',
                    url: 'style/ajax.php',
                    data: 'do=note&value=' + note + '&preid=' + preid,
                    success: function(data)
                    {
                        window.data = data;
                        alert(data);
                    }
                });
            });

全局变量是危险的,也许超出成功范围的变量就足够了
success回调之外的无功:

$("a.note").click(function( evt ){
                var note = $(this).attr("value");
                var preid = $(this).attr("id");
                var dataFromServer = null;
                $.ajax({
                    type: 'GET',
                    url: 'style/ajax.php',
                    data: 'do=note&value=' + note + '&preid=' + preid,
                    success: function(data)
                    {
                        dataFromServer = data;
                        alert(data);
                    }
                });
            });

最后一个选项是有一个隐藏的输入来存储数据;

                    success: function(data)
                    {
                        $('#hiddenFieldId').val(data);
                        alert(data);
                    }

注意事项:

  1. 我把你的选择器从a[class=note]改成了a.note,这样更好
  2. success是一个回调,这意味着在响应到达客户端之前不会触发它,在此之前,您的全局''外部var''隐藏输入值将为null。如果您不希望ajaxasynchronous,您可以在以下选项中定义它:

  $.ajax({
    async: false, //  <---
    type: 'GET',
    url: 'style/ajax.php',
    data: 'do=note&value=' + note + '&preid=' + preid,
    success: function(data)
    {
        dataFromServer = data;
        alert(data);
    }
});           
$("a[class=note]").click(function( evt ){
                    var note = $(this).attr("value");
                    var preid = $(this).attr("id");
                    $.ajax({
                        type: 'GET',
                        url: 'style/ajax.php',
                        data: 'do=note&value=' + note + '&preid=' + preid,
                        success: function(data)
                        {
                            window.data = data;
                        }
                    });
                });

当然,在回调触发之前,您不能使用它。

在所有代码之上定义var data = null;,它将是全局变量。之后,为流程函数和函数体中的参数重命名为window.data = response;

编辑

您可以定义触发数据更改的功能,例如:

var data = null;
function setGlobal(v) {
    window.data = v;
    alert(window.data);
}
$("a[class=note]").click(function( evt ){
    var note = $(this).attr("value");
    var preid = $(this).attr("id");
    $.ajax({
        type: 'GET',
        url: 'style/ajax.php',
        data: 'do=note&value=' + note + '&preid=' + preid,
        success: function(data){
            setGlobal(data);
        }
    });
});

试试看…