通过AJAX传递后的变量 [英] Passing post variables through ajax

查看:182
本文介绍了通过AJAX传递后的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接:

<a class="tag" wi_id="3042" wl_id="3693" for_user_id="441" href="#a">

这将触发Ajax调用

which triggers an ajax call

$(".tag").click(function() {
        var for_user_id = $(this).attr("for_user_id");
        var wl_id = $(this).attr("wl_id");
        var wi_id = $(this).attr("wi_id");
        $.ajax({
            type: "POST",
            url: "/ajax/actions/tag.php",
            data: { 
                'for_user_id': 'for_user_id', 
                'wl_id': 'wl_id',
                'wi_id': 'wi_id'
            },
            success: function(data){
                $(this).text("You've tagged this");
                $(this).closest('.gl_buttons_holder').toggleClass('gl_buttons_holder gl_buttons_holder_tagged');
                $(this).closest('.gl_buttons').addClass('tagged');
            }
        });
        return false;
    });

但在控制台中我看到以下内容:

But in the console I see the following:

TypeError: e is undefined

AJAX的文件被处理,但POST数据是空白,成功的行动不会发生,因此,它被张贴着零和类不改变

The ajax file gets processed but the POST data is blank, and the success actions do not happen, so it gets posted with zeros and classes are not changed

我瞪大了眼睛,盯着......任何明显?

I have stared and stared... anything obvious?

推荐答案

不会自动给AJAX回调函数传递。您可以使用背景:参数告诉jQuery来传递:

this is not passed automatically to the AJAX callback function. You can use the context: parameter to tell jQuery to pass it:

    $.ajax({
        type: "POST",
        url: "/ajax/actions/tag.php",
        data: { 
            'for_user_id': for_user_id, 
            'wl_id': wl_id,
            'wi_id': wi_id
        },
        context: this,
        success: function(data){
            $(this).text("You've tagged this");
            $(this).closest('.gl_buttons_holder').toggleClass('gl_buttons_holder gl_buttons_holder_tagged');
            $(this).closest('.gl_buttons').addClass('tagged');
        }
    });

这篇关于通过AJAX传递后的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆