Javascript jqueryajax在成功后将函数作为数据返回

Javascript jqueryajax在成功后将函数作为数据返回,javascript,php,jquery,ajax,codeigniter,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'); ?>'

我有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>
这只是带文本的字符串,成功后我用文本返回警报。我想拿回电话

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


我应该在数据中写什么?到目前为止,我找到的唯一解决方案是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'
                    });     
                }
            });
        }
    }
});

ajax向控制器发送数据??请求控制器提供的信息不发送…您的php文件是什么。php很好,我添加了
url:“index.php/mycontroller/function”,
立即检查谢谢您的帮助,但是现在每次单击我的函数。它总是返回相同的结果(res_no)。当它应该返回时(res_new),但是如果我执行以下操作:data:{res_no:res_no,res_new:res_new},success:function(data){alert(data);}并单击按钮,警报将显示正确的结果!这里出了什么问题?您得到相同结果的原因是,您需要插入从AJAX请求返回的任何内容。我不知道您从AJAX收到了什么,因为您还没有指定,所以我不知道。也许在你的控制器(AJAX使用的)中用PHP更新你的原始帖子,我得到一个数字,如果这个数字更高,那么1 res_new将设置FlashSeseion res_new,如果这个数字是0 res_no将发生。添加了更多的php。在我看来,您的php打印的是字符串,而不是数字。。AJAX将只接收函数打印或回显的内容。因此,在AJAX结果中,您必须捕获这些结果,并根据字符串编写条件语句。您可以使用
控制台.log
检查
数据
变量中的内容,您知道怎么做吗?非常感谢Diego通过Teamviewer帮助我,这对我来说是一个很好的选择。
$('#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({
    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'
                    });     
                }
            });
        }
    }
});