javascript - Ajax won't call Codeigniter controlllers's method -


i'm total beginner ajax (don't know jquery @ all) i've been using simple ajax without jquery, want simple call codeigniter's controller method. dont know i'm wrong at. here's ajax function , controller:

   function usernameonchange() {         var username = document.getelementbyid("register_username").value;         if (username.length == 0) {             document.getelementbyid("usernameglyph").classname = "glyphicon glyphicon-remove";             return;         } else {             var xmlhttp = new xmlhttprequest();             xmlhttp.onreadystatechange = function() {                 if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {                     document.getelementbyid("usernameglyph").classname  = 'glyphicon glyphicon-ok';                 }             };             var link = "<?php echo base_url("index.php/test/checkusername?username="); ?>" + username ;             xmlhttp.open("get", link, true);             xmlhttp.send();         }     } 

and here's controller (it's still test controller see ajax-codeigniter php connection working).

<?php  class test extends ci_controller {  public function __construct() {     parent::__construct();     $this->load->helper("url");     $this->load->library("form_validation");     $this->load->helper("security");     $this->load->helper("form"); }  public function checkusername($username) {     echo "<script>alert('codeigniter responded!');</scirpt>"; }  }  ?> 

thanks in advance!

before start ajax, need understand ajax required have output php perfect result of call. in codeigniter controller, echoing script tag. please dont when use ajax call.

sample codeigniter controller function

<?php  class test extends ci_controller {  public function __construct() {     parent::__construct();     $this->load->helper("url");     $this->load->library("form_validation");     $this->load->helper("security");     $this->load->helper("form"); }  public function checkusername($username) {     $output = array('status'=>200,"message"=>"your ajax called");     header('content-type:application/json;');//please not forgot set headers     echo json_encode($output); }  } 

here controller give perfect output javascript can read easiliy

for jquery

<script type="text/javascript">     $.get('<?php echo base_url("index.php/test/checkusername?username=xyz"); ?>',function(data){     alert(data['message']); }); </script> 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -