codeigniter:将数据从视图传递到控制器不工作

时间:2013-02-02 03:48:09

标签: php json codeigniter jquery

我在我的视图文件(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>

以下是我的控制器(searchC.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

?>

现在我已经使用开关对控制器进行了测试,以检查传递的json数据是否成功但是它总是显示未定义。这里有什么问题?有人可以解释为什么控制器中无法识别数据吗?

2 个答案:

答案 0 :(得分:1)

您没有通过网址传递数据,因此您需要使用$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;
}

答案 1 :(得分:0)

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

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

这对你有用。