I tried to pass a value to method of controller ( am using codeigniter ) using ajax through jquery. I called the ajax method with in a click event, the click event is working fine, but the ajax is not call. Plz help me. Thanks in advance.
$('#sql_format').click(function(){alert('hi');
$.ajax({
url : '<?php echo site_url('adhoc/sql_formater'); ?>',
data : '',
type: 'post',
dataType : 'json',
success : function(data){
alert(data);
}
});
});
when click the "#sql_format" it displays alert box with 'hi' message. But it doesn't call ajax. Here the adhoc is controller and sql_formater is method.
Controller code
function sql_formater(){
$sql = $this->input->post('query');
return $sql;
}
Try to set all parameters to ajax post like in example, see my answer here
var postData = new FormData();
$.ajax({
processData: false, /* important */
contentType: false, /* important */
type: "POST",
url: 'b.php',
data: postData, /* ** postData */
error: function(jqXHR, textStatus, errorThrown){ alert(textStatus); }, /* show error */
success:function(data, textStatus, jqXHR) { alert(data); }, /* show result*/
dataType: 'html' /* I change it ot html for this example*/
});
Controller code
function sql_formater()
{
$sql = $this->input->post('query');
header('Content-type: application/json');
echo json_encode(array("response"=>$sql));
exit;
}
And in view
$('#sql_format').click(function(){
$.ajax({
url : '<?php echo site_url("adhoc/sql_formater"); ?>',
data : {'query':'select * from table'},
type: 'post',
dataType : 'json',
success : function(data){
alert(data.response);
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.