无法让ajax工作编码器

Cannot get ajax to work codeigniter

本文关键字:工作 编码器 ajax      更新时间:2023-09-26

我有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');
    }
}

我想在javascript中获得值'test'。但是我不能让这个工作

我想下面的代码会对你有所帮助

<?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));
    }
}

代码必须在服务器端

是否包含了jquery库?就像

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