javascript - jQuery AJAX 在成功时将函数作为数据返回

标签 javascript php jquery ajax codeigniter

我有 codeigniter flashdata + 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 它将像我想要的那样向我显示通知但刷新..

最佳答案

为了实现这一点,您需要放置:

$('#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'
                    });     
                }
            });
        }
    }
});

关于javascript - jQuery AJAX 在成功时将函数作为数据返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31046361/

相关文章:

javascript - 在 div dwtcontrolContainer 中定义 ImageSize 动态 Web TWAIN SDK API

php - 使用 jquery ajax 将 javascript 对象转换为 php

php - 连接mysql超时

javascript - 如何从ajax获取表单ID并在symfony2的点击功能中设置它

php - 如何在 Symfony2 中将日期时间字段的默认值设置为当前时间?

javascript - .submit 在 javascript 中与 .submit 在 jquery 中

javascript - 将带有双引号和单引号的字符串从 asp 传递到 javascript 函数

jquery - JQuery 在 CSS 中更改背景颜色的问题

javascript - jQuery 数据从多个名称中选择

javascript - 调用 jQuery 对话框两次