编码点火器:将数据从视图传递到控制器不起作用


codeigniter : passing data from view to controller not working

我的视图文件(searchV.php)中有以下代码:

<html>
    <head> 
        <title>Search Domains</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            function noTextVal(){
                $("#domaintxt").val("");
            }
            function searchDom(){
                var searchTxt = $("#searchTxt").val();
                var sUrl = $("#url").val();
                $.ajax({
                    url : sUrl + "/searchC",
                    type : "POST",
                    dataType : "json",
                    data :  { action : "searchDomain", searchTxt : searchTxt },
                    success : function(dataresponse){
                        if(dataresponse == "found"){
                            alert("found");
                        }
                        else{
                            alert("none");
                        }
                    }
                });
            }
        </script>
    </head>
    <body>
        <form id="searchForm">
                <input type="text" id="searchTxt" name="searchTxt" onclick="noTextVal()" >
                <input type="submit" id="searchBtn" name="searchBtn" value="Search" onclick="searchDom()" />
                <input type="hidden" id="url" name="url" value="<?php echo site_url(); ?>" />
        </form>
        <?php
            var_dump($domains);
            if($domains!= NULL){
                foreach ($domains->result_array() as $row){
                    echo $row['domain'] . " " . $row['phrase1'];
                    echo "<br/>";
                 }
            }
        ?>
    </body>
</html>

下面是我的控制器(搜索C.php):

<?php
class SearchC extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('searchM');
    }
    public function index()
    {
        $data['domains'] = $this->searchM->getDomains();
        $this->load->view('pages/searchV', $data);

        switch(@$_POST['action']){
            case "searchDomain":
                echo "test";
                $this->searchDomains($_POST['searchTxt']);
                break;
            default:
                echo "test2";
                echo "<br/>action:" . ($_POST['action']);
                echo "<br/>text:" . $_POST['searchTxt'];
        }
    }
    public function searchDomains($searchInput)
    {
        $data['domains'] = $this->searchM->getDomains($searchInput);
        $res = "";
        if($data['domains']!=NULL){ $res = "found"; }
        else{ $res = "none"; }
        echo json_encode($res);
    }
} //end of class SearchC
?>

现在我已经使用 switch 在控制器上进行了测试,以检查通过的 json 数据是否成功,但它总是显示未定义。这是怎么回事?有人可以解释为什么控制器无法识别数据吗?

您不会通过 url 传递数据,因此您需要使用 $this->input->post() 来检索数据。

例如

public function searchDomains()
{
    $data['domains'] = $this->searchM->getDomains($this->input->post('searchTxt'));
    $res = "";
    if($data['domains']!=NULL){ $res = "found"; }
    else{ $res = "none"; }
    echo $res;
}
我相信

数据被正确返回,但问题出在您的代码检查上。$.ajax 函数解析 JSON 并将其转换为 JavaScript 对象。因此,您需要按如下方式修改代码:

if(dataresponse.res == "found"){ // Changed from dataresponse to dataresponse.res
   alert("found");
}
else{
  alert("none");
}

这应该适合您。