AJAX Post 没有发送正确的值
AJAX Post not sending the correct values
我正在尝试使用 AJAX 将数据从我的视图发送到我的控制器。完成后,我们的想法是将该数据转发到一个新视图,然后该视图将使用它来相应地显示信息。但是,当我尝试时,数据没有到达视图。奇怪的是,在使用 Chrome 的开发人员工具时,如果我 console_log AJAX 调用成功函数中的数据,它显示变量是正确的,但是当我尝试在 html 中显示该变量,它的值为 0。有什么想法吗?
我的视图函数:
$(document).on("keyup", function(e){
guess = String.fromCharCode(e.which).toLowerCase();
if (counter < test_key.length){
if (guess == 'f' || guess == 'j'){
if (guess == test_key[counter].answer){
console.log(counter);
console.log(test_key[counter]);
$(document).off('keyup');
$('#imageLocation').hide();
counter++;
practiceTask(counter, test_key, test_id);
}
else{
$.ajax({
type: "POST",
url: '<?php echo site_url('practiceTest/errorPage'); ?>',
data: {question: 6},
success: function(data){
console.log(data);
window.location.href = '<?php echo site_url('practiceTest/errorPage'); ?>';
}
});
}
}
}
});
我的控制器错误页面函数:
function errorPage(){
if (!empty($_POST['question'])){
$data['question'] = $this->input->post('question');
}
else{
$data['question'] = 0;
}
$this->load->view("practice_incorrect_view", $data);
}
还有我的新观点practice_incorrect_view:
<html>
<head>
</head>
<body>
<?php echo $question; ?>
<div class="centered">
<div style="width: 500px; margin: 20px auto;">
</div>
</div>
</body>
</html>
问题的根源是您成功发布到 practiceTest/errorPage
(因此控制台正确),但在成功回调中,您正在导航到 practiceTest/errorPage
,它不知道任何关于您刚刚发布的表单数据。
$.ajax({
type: "POST",
url: '<?php echo site_url('practiceTest/errorPage'); ?>',
data: {question: 6},
success: function(data){
console.log(data);
//-- this is the troublesome line
//-- window.location.href = '<?php echo site_url('practiceTest/errorPage'); ?>';
}
});
您应该使用从成功的 AJAX 请求返回的数据直接修改 DOM,而不是使用 window.location
重定向。
哪里有例子吗?
我正在尝试使用 AJAX 将数据从我的视图发送到我的控制器。完成后,我们的想法是将该数据转发到一个新视图,然后该视图将使用它来相应地显示信息。但是,当我尝试时,数据没有到达视图。奇怪的是,在使用 Chrome 的开发人员工具时,如果我 console_log AJAX 调用成功函数中的数据,它显示变量是正确的,但是当我尝试在 html 中显示该变量,它的值为 0。有什么想法吗?
我的视图函数:
$(document).on("keyup", function(e){
guess = String.fromCharCode(e.which).toLowerCase();
if (counter < test_key.length){
if (guess == 'f' || guess == 'j'){
if (guess == test_key[counter].answer){
console.log(counter);
console.log(test_key[counter]);
$(document).off('keyup');
$('#imageLocation').hide();
counter++;
practiceTask(counter, test_key, test_id);
}
else{
$.ajax({
type: "POST",
url: '<?php echo site_url('practiceTest/errorPage'); ?>',
data: {question: 6},
success: function(data){
console.log(data);
window.location.href = '<?php echo site_url('practiceTest/errorPage'); ?>';
}
});
}
}
}
});
我的控制器错误页面函数:
function errorPage(){
if (!empty($_POST['question'])){
$data['question'] = $this->input->post('question');
}
else{
$data['question'] = 0;
}
$this->load->view("practice_incorrect_view", $data);
}
还有我的新观点practice_incorrect_view:
<html>
<head>
</head>
<body>
<?php echo $question; ?>
<div class="centered">
<div style="width: 500px; margin: 20px auto;">
</div>
</div>
</body>
</html>
问题的根源是您成功发布到 practiceTest/errorPage
(因此控制台正确),但在成功回调中,您正在导航到 practiceTest/errorPage
,它不知道任何关于您刚刚发布的表单数据。
$.ajax({
type: "POST",
url: '<?php echo site_url('practiceTest/errorPage'); ?>',
data: {question: 6},
success: function(data){
console.log(data);
//-- this is the troublesome line
//-- window.location.href = '<?php echo site_url('practiceTest/errorPage'); ?>';
}
});
您应该使用从成功的 AJAX 请求返回的数据直接修改 DOM,而不是使用 window.location
重定向。
哪里有例子吗?