Ajax call using jQuery with response type json at CodeIgniter


Now Ajax call using jQuery is very easy and simple . Have a look the codes how easy you can manipulate your Ajax call with out Reloading your web page and get your desire results.

This code should Paste on your view page :

<script type=”text/javascript” src=”http://code.jquery.com/jquery.min.js”></script&gt;
<script  type=”text/javascript”>
jQuery(document).ready(function(){
jQuery(‘#buton’).click(function(){
jQuery.ajax({
url: “<?php echo site_url(‘login/ajaxcall’); ?>”,
type: ‘POST’,
data:{ id : 5 },
dataType: “json”,
success: function(data){
var val = “<select name=\”user\”>”+
“<option>Select from below</option>”+
“<option>”+data.id+”</option>”+
“<option>”+data.name+”</option>”+
“<option>”+data.age+”</option>”+
“<option>”+data.sex+”</option>”+
“</select>”;
jQuery(‘#ans’).html(val);
},
error: function(data){
alert(data);
}
});
return false;
});

});
</script>

<center>
<button id=”buton” >Click And Get Ajax Value</button>
<p id=”ans”></p>
</center>

Now look how you can response according to the ajax request.This Code should be Paste on CodeIgniter Controller :

<?php

class Login extends CI_Controller{

public function  __construct() {
parent::__construct();

}
public function ajaxcall(){

$data[‘id’]   = $this->input->post(‘id’);
$data[‘name’] = ‘Ziyed’;
$data[‘age’]  = ’24’;
$data[‘sex’]  = ‘Male’;

echo json_encode($data);

}

}

?>

 

 

 

Advertisements

2 responses to “Ajax call using jQuery with response type json at CodeIgniter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s