ajax callings to functions in the controller codei

2019-06-05 01:12发布

问题:

I am using codeigniter 3 and I am having a strange issue using ajax. I'm trying to call two ajax simultaneously and I thought I have problem with my ajax code and I asked this question here and nothing worked with me.

I kept making the code more simple and narrow it down until I discover that the problem seems to be with the codeigniter

Here is my js code:

        do_download();
        get_operation_status();

        function get_operation_status() {
            //var url_process_info = "http://www.example.com/public/sleep_status.php";// this url works as expected 
            var url_process_info = "<?php echo site_url(); ?>download/sleep_status";

            $.ajax({
                url: url_process_info,
                dataType: 'json',
                async: true,
                method: "GET",
                success: function (data) {

                },
                complete: function (){
                    get_operation_status();

                }
            });
        }


        function do_download(){
            //var download_url = "http://www.example.com/public/sleep_download.php"; // this url works as expected 
            var download_url = "<?php echo site_url(); ?>download/sleep_download";

            $.ajax({
                url: download_url,
                method: "POST",
                async: true

            });
        }

My php code, inside the Download controller, I have these two functions just for testing :

 public function  sleep_status () {
        sleep(1);
    }
    public function sleep_download(){
        sleep (7);
    }

Calling the ajax to the controller download, it showing the issue which keeps the second call pending until the first one is executed.

I created two php files and put them in /public/sleep_download.php and /public/sleep_status.php

and within the two files just the sleep line of code, and it works as I want, while the first call is running, the second ajax call has been excuted several times .

Edited: I could make it work by making the class not extending from CI_Controller, I created a new class named Action and I commented this part:

class Action /*extends CI_Controller*/ {

Although I could make it work, I still wonder why was it happening ? and is there a better solution ?