1

I have a problem with success function in my $.ajax function in javascript file:

$("#country select").change(function () { 
            var country_value = $(this).val(); 
            $.ajax({
                url:base_url + "Search_controller/testing_controller", 
                method: 'post',
                data: {country_val: country_value },
                dataType: 'json',
                success: function(data){
                    console.log('done : ' + data);  
                },
                   error: function (reponse) {
                console.log('Problem with ajax');
                }

            });

my Controller function

   <?php 

class Search_controller extends CI_Controller{

    public function index(){

    }


    public function testing_controller(){
        $data ="statessssssss";
        echo json_encode($data);
    }

  }
?>

**

The Problem is the codes do nothing, i don't know what the problem Always return to me in Browser log 'Problem with ajax'

**

3 Answers3

1

base_url() is a Codeigniter function (php), in your $ajax function you use the javascript variable base_url, which is not defined.

in order to get the php base_url() into your $ajax function you need to echo out the php function, changing to this line:

url: "<?php echo base_url() ?>Search_controller/testing_controller",
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • is a javascript file not php file – Abdelhakim Ezzahraoui Oct 12 '19 at 19:42
  • base_url is defined because i testing after $.ajax >> console.log(country_value + ' ' + base_url); and working done , the problem is the ajax with controller – Abdelhakim Ezzahraoui Oct 12 '19 at 19:44
  • I tried to explain: base_url is set in Codeigniter, right?, now you are trying to adress the value of base_url in javascript. but that doesn't work, you need to echo it out. I personally prefer just to use `url:"/Search_controller/testing_controller"` but wanted to explain, what you have to do when using base_url in javascript – Vickel Oct 12 '19 at 19:47
  • i change the url, same problem not reponse ,just console me "Problem with ajax " !!! – Abdelhakim Ezzahraoui Oct 12 '19 at 19:54
  • check naming convention: is it **S**earch_controller.php in your controller folder? maybe edit your question adding the controller code? – Vickel Oct 12 '19 at 19:58
  • yes of course it's "Search_controller.php" also name of Class "Search_controller" – Abdelhakim Ezzahraoui Oct 12 '19 at 20:01
1

Change your URL as follows

url:"<?php echo base_url()?>index.php/Search_controller/testing_controller",

I tested your code and its working file.

0

I find the solution is because of CSRF security

$("#country select").change(function () { 
             var country_value= $(this).val(); 
             var data = { /* params  */
                    "country": country_value,
                     "state": '001'
                };
              data[csfr_token_name] = $.cookie(csfr_cookie_name);
            $.ajax({
                url:base_url + "Search_controller/testing_controller", 
                method: 'post',
                data: data,
                dataType: 'json',
                success: function(data){
                    console.log('done : ' + data);  
                },
                   error: function (reponse) {
                console.log('Problem with ajax');
                }

            });

This Code Working