Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQueryPost和promise的Javascript范围问题_Javascript_Jquery - Fatal编程技术网

jQueryPost和promise的Javascript范围问题

jQueryPost和promise的Javascript范围问题,javascript,jquery,Javascript,Jquery,我正在尝试从jquery帖子的响应构建一个数组。我有以下代码: categories_names = []; $(categories).each(function(index, category_id) { $.post('/modules/blog/add_post_categories', {post_id:result.result, category_id:$(category_id).val()}, function(result) { cat

我正在尝试从jquery帖子的响应构建一个数组。我有以下代码:

categories_names = [];
$(categories).each(function(index, category_id) {
    $.post('/modules/blog/add_post_categories', {post_id:result.result, category_id:$(category_id).val()},
    function(result)
    {
        categories_names.push = result;
    });
}).promise().done(function() {
    var final_cats = categories_names.join("");
    console.info(final_cats);
});
post请求将类别id插入到一个表中,并返回该类别的名称,其格式类似于此处的类别名称,这样,一旦构建了数组并将所有选中的类别(通过前面表单中的复选框)输入到数据库中,就可以将该数组合并并作为html插入到最终视图中。此时,正如您所看到的,代码只是在控制台中打印出来,但正如您可能猜到的,它返回
(一个空字符串)

我知道这很可能是一个范围问题,因为这是javascript的一个领域,仍然让我感到困惑,所以我希望有人能对此有所了解

categories_names.push = result;
你能试着把这个改成

categories_names.push(result);
因为push是一种方法,而不是一个变量。

你的
.promise().done(…)
没有做你认为它是做的事情。它只是立即解析,因为在您迭代的元素上没有动画。另外,
[].push
的语法错误

var defArr = $(categories).map(function(index, category_id) {
    return $.post('/modules/blog/add_post_categories', {post_id:result.result, category_id:$(category_id).val()});
}).get();

$.when.apply(null,defArr).done(function() {
    //var categories_names = [];
    //for (var i = 0; i < arguments.length; i++) {
    //    categories_names.push(arguments[i][0]);
    //}
    var categories_names = $.map(arguments,function(i,arr) {
        return arr[0];
    });
    var final_cats = categories_names.join("");
    console.info(final_cats);
});
var defArr=$(类别).map(函数(索引,类别id){
返回$.post('/modules/blog/add_post_categories',{post_id:result.result,category_id:$(category_id).val()});
}).get();
$.when.apply(null,defArr).done(函数(){
//变量类别名称=[];
//for(var i=0;i
post是异步进行的吗?您确定
post
实际上返回了任何内容吗?你能在开发者工具中看到它吗?@MikeChristensen-我想没有。。。但这可以解释。。。我是否应该将
post
更改为
$.ajax
,这样我就可以控制它了?@MattBurland-是的。。。它肯定是在返回你的
.promise().done(…)
并不是在做你认为应该做的事。它只是立即解析,因为在您迭代的元素上没有动画。另外,您的
[].push
语法错误。哇。正如我上面的评论所解释的那样,这将解释为什么这一切都是无序发生的。谢谢你。我现在就要试试了——我不知道承诺只限于动画?更妙的是,你不需要手动将结果推送到
类别名称
数组。每个
$.post
的承诺都通过
[data,statusText,jqXHR]
格式的数组来解决。因此,您可以使用
$.map(参数,函数(x){return x[0]})从
done
回调中的promise结果中检索所有类别名称
@code>$.promise()为DOM元素动画生成承诺。AJAX请求本身已经实现了承诺,但是当您想要绑定到它们时,您需要确保遵守这些承诺!在您的原始代码中,没有任何内容跟踪这些承诺。最新的编辑使其以正确的顺序填充数组。在jQuery集合上调用.promise()时,它会从
“fx”
队列中运行。这就是为什么您的原始代码只跟踪动画。