0

Could someone figure out why every ajax call is leading to error. Here goes my problem. enter image description here

The code is

$(document).ready(function(){
$('#officer-id').change(function(){
    var officer_id = $('#officer-id').val();
    $.ajax({
      type:'POST',
      url:"<?=base_url()?>" + "Home/ajax_view",
      dataType: 'json',
      data:{'officer_id':officer_id},
      success:function(data){
                   alert(this.data ); 
       },
      error:function(data){
                alert("error");
      }


    });
});
});

the Id officer_id in the html code is

   <select id="officer-id" placeholder="Choose an officer">
               <option value=ab1>ab1</option><option value=ab2>ab2</option><option value=rep1>rep1</option>
            </select>

the url:"<?=base_url()?>" + "Home/ajax_view" content

  public function ajax_view(){
    return ;
  }

Why my code is not executing success inspite of getting no error in console

Raghib Ahsan
  • 396
  • 2
  • 4
  • 14

1 Answers1

0

try this:

$('#officer-id').change(function(){
    var officer_id;
    if(officer_id = $(this).val()){ //
        $.ajax({
          type:'POST',
          url:"http://62.231.118.52:9080/teste/random_test/form_validate.php",
          dataType: 'json',
          data:{'officer_id':officer_id},
          success:function(data){
                       alert(this.data ); 
           },
          error:function(data){
                    alert("error");
          }


        });
    }
});
Vali S
  • 1,471
  • 2
  • 10
  • 18