Javascript 通过ajax传递post变量
我有一个链接:Javascript 通过ajax传递post变量,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个链接: <a class="tag" wi_id="3042" wl_id="3693" for_user_id="441" href="#a"> 但在控制台中,我看到以下内容: TypeError: e is undefined ajax文件得到处理,但POST数据为空,并且不会发生成功操作,因此它会以零进行POST,并且不会更改类 我一直盯着看。。。有什么明显的问题吗?您发送的数据有误,不要在单引号内调用变量 $(".tag").click(function() {
<a class="tag" wi_id="3042" wl_id="3693" for_user_id="441" href="#a">
但在控制台中,我看到以下内容:
TypeError: e is undefined
ajax文件得到处理,但POST数据为空,并且不会发生成功操作,因此它会以零进行POST,并且不会更改类
我一直盯着看。。。有什么明显的问题吗?您发送的数据有误,不要在单引号内调用变量
$(".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;
});
这不会自动传递给AJAX回调函数。您可以使用context:参数告诉jQuery传递它:
$.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');
}
});
“for_user_id”:for_user_id,而不是“for_user_id”:“for_user_id”,我在您的代码中没有看到任何会导致该错误的内容,除非该错误是由您尚未提供的服务器代码引起的。这看起来像是Javascript错误,而不是PHP错误。但是代码中没有e变量。@KevinB它在回调中使用$this而没有-context:this-这导致了错误,非常感谢-单引号和变量,doh!我盯着看了又盯着看谢谢,我不知道“这个”没有被传给回调,我现在正在读它-当我可以的时候会接受的
$.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');
}
});