将 JavaScript 变量传递到 Ajax 数据中:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14718045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:23:39  来源:igfitidea点击:

Passing JavaScript Variable into Ajax data:

phpjavascriptjqueryajax

提问by Josh Kissel

I am working on a jQuery sortable. I pull information from my database to populate each orderable element and then repost the new order back to my data base. However I cannot figure out how to take the variable that I define in my serialize jQuery and enter it into my AJAX function in the data: area.

我正在研究可排序的 jQuery。我从我的数据库中提取信息来填充每个可订购元素,然后将新订单重新发布回我的数据库。但是,我无法弄清楚如何获取我在序列化 jQuery 中定义的变量并将其输入到我的 AJAX 函数中的 data: 区域。

Here is my code:

这是我的代码:

$(document).ready(function() 
{              
    $('#social_list').sortable(
    { 
        update: function() 
        {
            var order = $('#social_list').sortable('serialize');
            alert(order);
        }                         
    });
});

$.ajax(
{
    url: 'save_items_order.php',
    data: ?,
    type: '$_POST', 
    success: function(msg)
    {
        alert(msg);
    }
});

So I want to take the string that var order creates and pass it into the ajax so data: = var order string

所以我想把 var order 创建的字符串传递给 ajax so data: = var order string

I tried data: $('#social_list').sortable('serialize'), everything seemed to work it just doesn't seem to update my table

我试过数据:$('#social_list').sortable('serialize'),一切似乎都有效,只是似乎没有更新我的表

采纳答案by Juan Mendes

Simplest solution is to make a global

最简单的解决方案是创建一个全局

$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            // global. you could just omit var, but I find this clearer
            window.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: window.order, /* ...rest of code */
});

But I'm sure you know globals are bad, you can at least contain them to your namespace,

但是我确定您知道全局变量很糟糕,您至少可以将它们包含在您的命名空间中,

var myNs = {};
$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            myNs.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: myNs.order /* ... rest of code */
});

or re-organize your code so they can share variables.

或重新组织您的代码,以便他们可以共享变量。

$(document).ready(function() {
    var order;
    $('#social_list').sortable({
        update: function() {
            order = $('#social_list').sortable('serialize');
        }
    });
    $('#myButton').click(function(){
        $.ajax({
            url: 'save_items_order.php',
            data: order, /* ... rest of code */
        });
    });
});

Note that for this case, you could just do the following

请注意,对于这种情况,您可以执行以下操作

$.ajax({
    url: 'save_items_order.php',
    data: $('#social_list').sortable('serialize') /* ... rest of code */
});

回答by Supericy

You just pass any javascript object in as the data.

您只需将任何 javascript 对象作为数据传入。

For your specific case, I would probably wrap the AJAX call in a function, like:

对于您的具体情况,我可能会将 AJAX 调用包装在一个函数中,例如:

function saveItemOrder(order)
{
    $.ajax({
        url: 'save_items_order.php',
        data: { "order": order },
        type: 'POST', // <-- this should be just "POST"
        success: function(msg){
            alert(msg);
        }
    });
}

And then:

接着:

update: function() {
    var order = $('#social_list').sortable('serialize');
    // alert(order);

    saveItemOrder(order);
}