성공 함수가 실행 중이므로 내 JQuery 코드가 게시됩니다. 문제는 PHP $ _POST 요청이 데이터를받지 못한다는 것입니다. 디버깅 목적을 위해 alert (data)를 사용하여 배열이 될 것으로 기대되는 것을 표시하려고 시도하고 전달 된 내용을 파싱하는 방법이 있다면 표시합니다. 대신 경고 상자에 "[object Object]"가 표시됩니다. 이 데이터를 PHP에서 콜백 함수로 사용할 수있는 형식으로 가져와야합니다.AJAX를 사용하여 여러 JQuery 값을 POST하고 개체 오류를 방지하는 방법
$(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 함수입니다 :
다음은 jQuery의
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
}
방화범은 매우 도움이되었습니다. AJAX 호출이 올바른 데이터를 게시하는 것 같습니다. POST 탭에서 다음과 같이 보았습니다. "action : \t cb_lud_arrow_action arrow : \t up entryID : \t 36". 그래서 나는 데이터베이스가 업데이트되지 않는 이유에 대해 여전히 분실되어 있습니다. 그러나 적어도 이것은 가능한 한 가지 문제를 배제합니다. 감사! – ChrisBuck