Javascript 当我试图从codeigniter中的控制器文件返回数据时,为什么ajax post方法返回空警报框
这是我的ajax函数Javascript 当我试图从codeigniter中的控制器文件返回数据时,为什么ajax post方法返回空警报框,javascript,codeigniter,Javascript,Codeigniter,这是我的ajax函数 $(function(){ $("#myform").submit(function(){ //i want to get data from my form after submittingit dataString = $("#myform").serialize(); alert(dataString); $.ajax({ type: "POST", url
$(function(){
$("#myform").submit(function(){ //i want to get data from my form after submittingit
dataString = $("#myform").serialize();
alert(dataString);
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData",
data: dataString,
success: function(data){
alert(data);
}
});
return false;
});
});
另一件事是我试图警告从表单序列化中获取的数据字符串,但它也是空的。我想在从javascript文件发送关键字后从数据库返回数据集。我尝试了javascript文件是否与我的控制器函数正确连接。所以我尝试返回一些值,并使用警报框显示它。但是它给出的结果是空的。您不能提醒$(“#myform”).serialize()代码>因为这是一个对象而不是警报对象,请尝试console.log($(“#myform”).serialize();)
然后打开浏览器控制台,查看打印的内容
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData",
data: dataString,
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data);
}
});
$.ajax({
类型:“POST”,
url:“index.php/customer/viewCustomerData”,
数据:dataString,
})
.完成(功能(数据){
if(console&&console.log){
日志(“数据样本:”,数据);
}
});
这是使用ajax提交表单的更好方法
$(document).on("submit", "#myform", function(event)
{
event.preventDefault();
$.ajax({
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData"
type: "POST",
data: new FormData(this),
processData: false,
contentType: false,
success: function (data, status)
{
alert(data);
},
error: function (xhr, desc, err)
{
}
});
});
$(文档)。在“提交”、“我的表格”上,函数(事件)
{
event.preventDefault();
$.ajax({
url:“index.php/customer/viewCustomerData”
类型:“POST”,
数据:新表单数据(本),
processData:false,
contentType:false,
成功:功能(数据、状态)
{
警报(数据);
},
错误:函数(xhr、desc、err)
{
}
});
});
试试这个
$(function(){
$("#myform").submit(function(){
dataString = $("#myform").serialize();
alert(dataString);
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData",
data: dataString,
cache: false,
datatype: 'json',
success: function(data){
alert(data.result);
}
});
});
});
我试过了。但它不起作用。我想做的是通过ajax post方法将值从输入字段发送到数据库。并将相关数据行从数据库返回到视图文件中的表中。控制台中不显示任何内容。仅显示表单序列化的“”
$(function(){
$("#myform").submit(function(){
dataString = $("#myform").serialize();
alert(dataString);
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData",
data: dataString,
cache: false,
datatype: 'json',
success: function(data){
alert(data.result);
}
});
});
});
function viewCustomerData(){
if($this->input->post('name'){
$result = $this->input->post('name');
} else {
$result = false;
}
header('Content-Type: application/json');
echo json_encode(array('result' => $result));
}