1

我的视图文件中有这段代码(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

?>

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

4

2 回答 2

1

您没有通过 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;
}
于 2013-02-02T04:01:34.477 回答
0

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

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

这应该适合你。

于 2013-02-02T08:34:40.273 回答