jQuery AJAX在成功后将函数作为数据返回

3

我有一个CodeIgniter闪存数据 + jQuery AJAX调用来显示它。 代码如下:

<script type="application/javascript">

var res_no = '<?php echo $this->session->flashdata('res_no'); ?>';
var res_new = '<?php echo $this->session->flashdata('res_new'); ?>';

(function( $ ) {

$("#check-reservations").click(function() {

   $.ajax({
            type: "POST",
            url: "mycontroller/function",
            async: true,
            data: {
            res_no: res_no,
            res_new: res_new
            },
            success: function(data) {   
            if(data) {
            alert(data);
            }
           }
        });
     });

/*
Default Notifications
*/
$('#check-reservations').show(function() {

    if (res_no) {
    new PNotify({
        title: 'Hmm no new Reservations..',
        text: res_no,
        type: 'custom',
        addclass: 'notification-primary',
        icon: 'fa fa-info-circle '
    });
    }

    else if (res_new) {
    new PNotify({
        title: 'There\'s something new!',
        text: res_new,
        type: 'custom',
        addclass: 'notification-success',
        icon: 'fa fa-check'
    });
    }
   });
   }).apply( this, [ jQuery ]);
 </script>

在 $.ajax 数据中,我已经添加了两个


res_no: res_no,
res_new: res_new

这些只是包含文本的字符串,在成功时,我会返回带有文本的警告。我想要获取返回的

new PNotify({
        title: 'Hmm no new Reservations..',
        text: res_no,
        type: 'custom',
        addclass: 'notification-primary',
        icon: 'fa fa-info-circle '
    });

PHP:

 /**
    * @filtered_reservations
    * @index
    */
    $filtered_reservations = $this->filter_array($reservations);          
    if (count($filtered_reservations) > 0) {
    foreach ($filtered_reservations as $index => $reservation) {
    $this->db->insert('reservations', $reservation);   
    } // end foreach   
    return $this->session->set_flashdata('res_new', "Success ". count($filtered_reservations) ." Reservations were Inserted!"); 
    print "Success ". count($filtered_reservations) ." Reservations were Inserted!";
    print "Reservations: ". count($kigores_id) ." found on kigo!";
    } /* end if */


    /**
    * @filtered_reservations
    * equal to 0
    */
    elseif (count($filtered_reservations) === 0) {
    print "Sorry no new Reservations!";
    return $this->session->set_flashdata('res_no', 'Sorry no new Reservations!');
    //$this->ci_alerts->set('warning', "Sorry no new Reservations!");
    }

    } /* end reservations */     

我应该在数据中写什么?到目前为止,我找到的唯一解决方案是window.reload,它会像我想要的那样显示通知,但需要刷新页面。


你的 PHP 文件是什么? - Harigovind R
PHP没问题,我已经添加了。 - Ilanus
url: "<?php echo base_url(); ?>index.php/mycontroller/function",现在检查。 - Abdulla Nilam
一切都正常工作。但是,我看到的是“Alert”而不是实际的PNotify,因为我不知道在AJAX成功时要放什么。 - Ilanus
我需要展示:new PNotify({ title: '嗯,没有新的预订..', text: res_no, type: 'custom', addclass: 'notification-primary', icon: 'fa fa-info-circle ' }); - Ilanus
显示剩余3条评论
1个回答

1
为了实现这一点,您需要放置这个:
$('#check-reservations').show(function() {
    if (res_no) {
        new PNotify({
            title: 'Hmm no new Reservations..',
            text: res_no,
            type: 'custom',
            addclass: 'notification-primary',
            icon: 'fa fa-info-circle '
        });
    } else if (res_new) {
        new PNotify({
            title: 'There\'s something new!',
            text: res_new,
            type: 'custom',
            addclass: 'notification-success',
            icon: 'fa fa-check'
        });
    }
});

在您的 AJAX 的 success 结果中,像这样:
$.ajax({
    type: "POST",
    url: "mycontroller/function",
    async: true,
    data: {
        res_no: res_no,
        res_new: res_new
    },
    success: function(data) {   
        if(data) {
            $('#check-reservations').show(function() {
                if (res_no) {
                    new PNotify({
                        title: 'Hmm no new Reservations..',
                        text: res_no,
                        type: 'custom',
                        addclass: 'notification-primary',
                        icon: 'fa fa-info-circle '
                    });
                } else if (res_new) {
                    new PNotify({
                        title: 'There\'s something new!',
                        text: res_new,
                        type: 'custom',
                        addclass: 'notification-success',
                        icon: 'fa fa-check'
                    });     
                }
            });
        }
    }
});

感谢您的帮助,但现在每次我点击我的函数时,它总是返回相同的结果(res_no),而不是应该返回(res_new)。但是,如果我这样做: data: { res_no: res_no, res_new: res_new },success: function(data) { alert(data); }并单击按钮,警报将显示正确的结果!这里有什么问题? - Ilanus
你得到相同的结果是因为你需要插入从AJAX请求返回的任何内容。我不知道你从AJAX收到了什么,因为你没有指定,所以我不知道。也许更新你原来发布的帖子,包括控制器中的PHP代码(AJAX使用的那个)。 - CodeGodie
如果返回的数字大于1,则res_new将设置flashsesion res_new;如果数字为0,则会发生res_no。这是更多PHP代码。 - Ilanus
使用console.log没有任何反应!我在我的控制器函数中设置了2个flash会话。1 res_no,2 res_new 在ajax中写的是: 如果(res_no)做某事,否则如果(res_new)做某事。 然而,似乎我没有比较任何东西?或者控制器没有把数字还给我?$filtered_reservations是预订数量,我如何将此变量传递到我的AJAX中?这样我就可以使用它进行比较了?感谢您的时间和帮助! - Ilanus
1
非常感谢Diego通过Teamviewer帮助我,这对我来说是一次很棒的学习经历。 - Ilanus
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接