简体   繁体   English

无法使Ajax正常工作

[英]Cannot get ajax to work codeigniter

I have the ajax code 我有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);
     }
});

In the php controller 在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');

    }
}

I wanna get the value 'test' in the javascript.But i cant get this working 我想在javascript中获取值“ test”。但是我无法正常工作

I guess following code will help you 我想下面的代码将帮助您

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

    }
}

the code must be in server side 该代码必须在服务器端

Have you included a jquery library? 您是否包含一个jQuery库? something like 就像是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM