我无法将AJAX请求发送到我的控制器

时间:2016-06-27 10:48:38

标签: javascript jquery ajax codeigniter bootstrap-modal

我有一个包含表单的模态,当我尝试使用ajax发送数据时,我失败了。这是我的控制器:

public function server()
{
    $name = $_REQUEST['nom'];
    $ip = $_REQUEST['ip'];
    $ftp = $_REQUEST['ftp'];
    $user = $_REQUEST['user'];
    $pass = $_REQUEST['pass'];
    $sql = "INSERT INTO servers (id, name, ip, ftp, user, pass, active) VALUES (NULL, '$name', '$ip', '$ftp', '$user', '$pass', '1');";
    echo $sql;
    $this->db->query($sql);
}
从我的浏览器的consol中我也没有错误,当我试图展示发生了什么时,就像告诉我一切都好。

这是我的观点:

                                                                                                        ×                             Ajoute nouveau serveur:                                                                                                                                                                                              喃                                                                                                                                                                                                                                                                                                        IP                                                                                                                                                                                                                                                                                                        FTP                                                                                                                                                                                                                                                                                                        Nom d'utilisateur                                                                                                                                                                                                                                                                                                        Mot de passe                                                                                                                                                                                                                                                                                    ANNULE                                                                                                                                                                         

    <!-- BEGIN JAVASCRIPT -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="assets/js/libs/jquery/jquery-migrate-1.2.1.min.js"></script>
    <script src="assets/js/libs/bootstrap/bootstrap.min.js"></script>
    <script src="assets/js/libs/spin.js/spin.min.js"></script>
    <script src="assets/js/libs/autosize/jquery.autosize.min.js"></script>
    <script src="assets/js/libs/nanoscroller/jquery.nanoscroller.min.js"></script>
    <script src="assets/js/core/source/App.js"></script>
    <script src="assets/js/core/source/AppNavigation.js"></script>
    <script src="assets/js/core/source/AppOffcanvas.js"></script>
    <script src="assets/js/core/source/AppCard.js"></script>
    <script src="assets/js/core/source/AppForm.js"></script>
    <script src="assets/js/core/source/AppNavSearch.js"></script>
    <script src="assets/js/core/source/AppVendor.js"></script>
    <script src="assets/js/core/demo/Demo.js"></script>
    <!-- END JAVASCRIPT -->
    <script type="text/javascript">
        $(document).ready(function(){
            $('form.newserverfrm').on('submit', function(form){
                form.preventDefault();
                $.post('/add/server', $('form.newserverfrm').serialize(), function(data){
                    alert(data);
                });
            });
        });
    </script>

3 个答案:

答案 0 :(得分:0)

您可以更改:

$.post('/add/server', $('form.newserverfrm').serialize(), function(data){...

$.post('<?php site_url("add/server") ?>', $('form.newserverfrm').serialize(), function(data){...

答案 1 :(得分:0)

你应该采取行动

更改

 $.post('/add/server', $('form.newserverfrm').serialize(), function(data){...

$.post('server', $('form.newserverfrm').serialize(), function(data){...

答案 2 :(得分:0)

请使用这些代码重新放置代码

查看页面

! - BEGIN FORM MODAL MARKUP - &gt;                                                                                     ×                         Ajoute nouveau serveur:                                                                                                                                                                      喃                                                                                                                                                                                                                                                                        IP                                                                                                                                                                                                                                                                        FTP                                                                                                                                                                                                                                                                        Nom d'utilisateur                                                                                                                                                                                                                                                                        Mot de passe                                                                                                                                                                                                                                                    ANNULE                                                                                                                                     

<!-- BEGIN JAVASCRIPT -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="assets/js/libs/jquery/jquery-migrate-1.2.1.min.js"></script>
<script src="assets/js/libs/bootstrap/bootstrap.min.js"></script>
<script src="assets/js/libs/spin.js/spin.min.js"></script>
<script src="assets/js/libs/autosize/jquery.autosize.min.js"></script>
<script src="assets/js/libs/nanoscroller/jquery.nanoscroller.min.js"></script>
<script src="assets/js/core/source/App.js"></script>
<script src="assets/js/core/source/AppNavigation.js"></script>
<script src="assets/js/core/source/AppOffcanvas.js"></script>
<script src="assets/js/core/source/AppCard.js"></script>
<script src="assets/js/core/source/AppForm.js"></script>
<script src="assets/js/core/source/AppNavSearch.js"></script>
<script src="assets/js/core/source/AppVendor.js"></script>
<script src="assets/js/core/demo/Demo.js"></script>
<!-- END JAVASCRIPT -->
<script type="text/javascript">

功能测试()         {

        $.ajax({
        type : 'POST',
        url : 'server.php',
        data : { },  
        async : false,
        success : function(data)
        {
            alert(data);
        }

        });

    }
</script>