Ajax返回数据错误

时间:2014-01-22 12:52:00

标签: javascript php jquery ajax codeigniter

我在codeigniter中从复选框中检索数据时遇到问题。 我的观点是

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script>
                var  base_url = '<?=base_url()?>';

                    function test()
                    {

                        var country = document.forms["fetch_country"].country;
                        var countryText = ''
                        for (i=0;i<country.length;i++)
                          {
                          if (country[i].checked)
                            {
                            countryText = countryText + country[i].value + ","

                            }
                          }
                        //  alert(countryText)

                                var type= countryText;  
                            //      alert (type);                

                       $.ajax({
                            url: base_url+'/index.php/ajax_test/index',
                            type : 'POST', //the way you want to send data to your URL
                            data : {'type' : type},
                            success : function(result){ //probably this request will return anything, it'll be put in var "data"
                                alert (type);
                                 $('#div1').html(result); //jquery selector (get element by id)

                            }
                        });
                          //  alert ("countryFinalValue is " + id);
                     }

        </script>               
</head>                 
                    <body>


                    <form id="fetch_country">


                    <div id="new">
                    <input id="country" type="checkbox" onclick="test()" value="1">India<br/>
                    <input id="country" type="checkbox" onclick="test()" value="2">USA<br/>
                    <input id="country" type="checkbox" onclick="test()" value="3">UK<br/>
                    <input id="country" type="checkbox" onclick="test()" value="4">China<br/>   

                    </div>
                    </form>

                        <div id="div1"></div>
                    </body>

AND我的控制器是:

<?php

class ajax_test extends CI_Controller
{
function __construct()
{

    parent::__construct();
    $this->load->view('view_test');
}   

public function index()
{
    //$this->load->view('view_test');
    $id = $this->input->post('type');
        echo $id;
    //$this->load->view('view_test' , $id );

}


}

?>

我面临的问题是我获得了复选框的值,但是有价值的我在我的视图中重复了整套复选框。

Output

1 个答案:

答案 0 :(得分:0)

请让您的列表生成如下

<input id="country" type="checkbox" data-value="1">India<br/>
<input id="country" type="checkbox" data-value="2">USA<br/>
<input id="country" type="checkbox" data-value="3">UK<br/>
<input id="country" type="checkbox" data-value="4">China<br/> 

您的测试控制器现在可以正常使用,但稍后将其添加到构造函数

public function __construct() {
    parent::__construct();
    if (!$this->input->is_ajax_request()) {

        redirect(); //make sure that user is redirected when request is not AJAX

    }

    $this->output->enable_profiler(FALSE); //force to disable profiler
}

您的javascript文件应该看起来很像,也可以在页面顶部的某处定义base_url

$(document).ready(function(){
    $('#country').click(function() {
            var value = $(this).data("value");
            $.ajax({ 
                type        : 'POST', //Method type
                url         : base_url + '/index.php/ajax_test/',
                data        : {type: value}, //post data
                success     : function(data) {
                    console.log(data);
                }
            });
        });
});