我试图通过jquery使用ajax将值传递给控制器的方法(我使用codeigniter)。我在单击事件中调用了 ajax 方法,单击事件工作正常,但没有调用 ajax。请帮助我。提前谢谢。
$('#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);
}
});
});
单击"#sql_format"时,它会显示带有"HI"消息的警报框。但它不叫ajax。这里的临时是控制器,sql_formater是方法。
控制器代码
function sql_formater(){
$sql = $this->input->post('query');
return $sql;
}
尝试将所有参数设置为 ajax 帖子,如示例所示,请参阅我的答案 这里
通过 Ajax 发送的上传文件名
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*/
});
控制器代码
function sql_formater()
{
$sql = $this->input->post('query');
header('Content-type: application/json');
echo json_encode(array("response"=>$sql));
exit;
}
在视野中
$('#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);
}
});
});