0

我认为 CI 返回所有页面,我需要字符串“GOOD”。

这是一张图片。http://d.pr/i/h4oi 请帮帮我)这是我对 Codeigniter 的看法:

<html>
<head>
<script src="<?php echo base_url(); ?>js/jquery.js" type="text/javascript"></script>
</head>
<body>

<input type="text" id="username" /> 
<input type="button" id="check" value="Check" />
<div id="name_feedback"></div>

<script>
$("#check").click(function() {
    $.post("<?php echo base_url(); ?>main/about", { name : $("#username").val() }, function(data) {
        $("#name_feedback").html(data);
        console.log(data.length);
    } );
} );
</script>

</body>
</html>

控制器:

function about() 
    {
        if (isset($_POST['name'])) {
            $name = $_POST['name'];
            $this->db->where('username', $name);
            $users = $this->db->get('users');
            if ($users->num_rows() > 0) {
                echo "Bad";
            } else 
            {
                echo "Good";
            }
        }
        $this->load->view('about_view');
    }

Codeigniter 返回#name_feedback 中的所有代码页,并回答“好或坏”

这是控制台日志:

Good<script src="http://localhost/project/js/jquery.js" type="text/javascript"></script>
<html>
<head>
<script src="http://localhost/project/js/jquery.js" type="text/javascript"></script>
</head>
<body>

<input type="text" id="username" /> 
<input type="button" id="check" value="Check" />
<div id="name_feedback"></div>

<script>
$("#check").click(function() {
    $.post("http://localhost/project/main/about", { name : $("#username").val() }, function(data) {
        $("#name_feedback").html(data);
        console.log(data);
    } );
} );
</script>

</body>
</html>
4

1 回答 1

1

您无需在 ajax 请求中加载整个视图,只需回显您的结果并调用die()或退出

function about() 
    {
        if (isset($_POST['name'])) {
            $name = $_POST['name'];
            $this->db->where('username', $name);
            $users = $this->db->get('users');
            if ($users->num_rows() > 0) {
                echo "Bad";
            } else 
            {
                echo "Good";
            }
             //check for post
            die();// or exit
        }

       // $this->load->view('about_view');
    }
于 2013-10-19T20:25:03.767 回答