gpt4 book ai didi

php - 如何使用 AJAX 发布多个 JQuery 值并避免对象错误

转载 作者:行者123 更新时间:2023-11-30 13:19:05 26 4
gpt4 key购买 nike

我的 JQuery 代码正在发布,因为成功函数正在触发。问题是 PHP $_POST 请求没有收到数据。出于调试目的,我尝试使用 alert(data) 来显示我希望的数组,并告诉我是否有办法解析传递的任何内容。相反,警告框只显示“[object Object]”。我需要将这些数据转化为可在 PHP 中用于回调函数的表单。

这是 JQuery:

$(document).ready(function(){
//JQuery for the submission of a new list item.
$('input.[class$="-arrow"]').click(function(e){
e.preventDefault(); //put e in function.
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';

if ($(this).hasClass('up-arrow')) {
var arrowdirection = 'up';
var entry = $(this).val();
}
else if ($(this).hasClass('down-arrow')) {
var arrowdirection = 'down';
var entry = $(this).val();
}

var data = {
action: 'cb_lud_arrow_action',
arrow: arrowdirection,
entryID: entry
};

$.ajax ({
type: 'POST',
url: ajaxurl,
data: data,
datatype: "text",
success: function(){
alert(data);
alert('Thanks for the vote!'); //for debug. Alert is showing! Still not submitting data to database though.
$('.line-items-rows').fadeOut('fast').delay(1000).fadeIn('fast');
}
});
});
});

这里是回调 PHP 函数(它没有按预期更新数据库):

function cb_lud_arrow_callback(){       
//Redefine basic variables
global $wpdb;
$cb_lud_prefix = $wpdb->prefix;
$cb_lud_table = $cb_lud_prefix . 'cb_list_up_down';
//find the values posted to the form
$cb_lud_arrow_keys = array_keys($_POST['arrow']);
$cb_lud_arrow_values = array_values($_POST['entryID']);
$cb_lud_entry_id = (int)$cb_lud_arrow_values[2];


//set some variables to "up" or "down" depending on form input.
if (strpos($cb_lud_arrow_keys[2], 'up-ID') > 0) {
$cb_lud_arrow_direction = "up";
}
else if (strpos($cb_lud_arrow_keys[2], 'down-ID') > 0) {
$cb_lud_arrow_direction = "down";
}
else {
$cb_lud_arrow_direction = "nothing";
}

//Create the update query that will record the up and down votes.
//Up votes
if ($cb_lud_arrow_direction == "up" && $cb_lud_arrow_direction != "nothing") {
$wpdb->query('UPDATE '.$cb_lud_table.' SET up_votes=up_votes+1
WHERE entry_ID='.$cb_lud_entry_id.'');
}
//Down votes
else if ($cb_lud_arrow_direction == "down" && $cb_lud_arrow_direction != "nothing") {
$wpdb->query('UPDATE '.$cb_lud_table.' SET down_votes=down_votes+1
WHERE entry_ID='.$cb_lud_entry_id.'');
}

die(); // this is required to return a proper result
}

最佳答案

使用 Firefox 并安装 Firebug。然后转到 Firebug 控制台并执行 jQuery 事件。它将向您显示所有 AJAX 交易、发送的内容、标题、帖子等,以及收到的内容。

另外,你真的可以缩短你的 AJAX 调用:

$.post(ajaxurl, data, function(data){
$('.line-items-rows').fadeOut('fast').delay(1000).fadeIn('fast');
});

关于php - 如何使用 AJAX 发布多个 JQuery 值并避免对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com