无法让ajax工作codeigniter

时间:2014-05-03 09:37:41

标签: javascript php jquery ajax codeigniter

我有ajax代码

      $.ajax({
     url: '<?php echo base_url(); ?>deleteRowUsingApiKey/index', //This is the current doc
     type: "POST",
     //dataType:'json', // add json datatype to get json
     data: {name: '145'},
     success: function(data){
         console.log(data);
         alert(data);
     }
});

在php控制器中

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
ini_set('display_errors', 1);
class DeleteRowUsingApiKey extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
            echo json_encode('test');

    }
}

我想获得价值&#39;测试&#39;在javascript.But我不能让这个工作

2 个答案:

答案 0 :(得分:0)

我想以下代码可以帮助您

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
ini_set('display_errors', 1);
class DeleteRowUsingApiKey extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
           //get data from ajax
           //if you send request with post
           $name = $this->input->post('name');

           //if you send request with get
           $name = $this->input->get('name');

           // create bject for returning data
           $return = array();
           $return['status'] = 'OK';
           $return['msg']    = 'some_message';      


           $this->output->set_content_type('application/json')
                ->set_output(json_encode($return));

    }
}

代码必须在服务器端

答案 1 :(得分:0)

你是否包含了一个jquery库?

之类的东西
    <script type="text/javascript" src="<?php echo base_url('js/jquery-1.10.1.min.js'); ?  >"></script>