Arrays 在codeigniter中使用ajax发送数组
如何使用ajax传递数组? 查看我的代码:Arrays 在codeigniter中使用ajax发送数组,arrays,ajax,codeigniter,Arrays,Ajax,Codeigniter,如何使用ajax传递数组? 查看我的代码: $(document).ready(function(){ $('#btn-send').click(function(){ var value = { 'name': $('#name').val(), 'phone': $('#phone').val(), 'mail': $('#mail').val(), }; $.a
$(document).ready(function(){
$('#btn-send').click(function(){
var value = {
'name': $('#name').val(),
'phone': $('#phone').val(),
'mail': $('#mail').val(),
};
$.ajax({
type: 'POST',
url: '<?php echo site_url('customer_add/test'); ?>',
data: value,
success: function(resp){
$('#error').html(resp);
}
});
});
});
在params中-在inspect元素中-有数据,但作为响应,没有返回任何内容。
发生了什么?
提前感谢。您通过ajax发送数据,并在需要时使用key:value对创建一个值数组 使用post将数据发送到服务器,以便获得整个阵列 在单个变量中使用
public function test5(){
$data = $this->input->post();
$name = $data['name']; echo $name;
$phone = $data['phone']; echo $phone;
$mail = $data['mail']; echo $mail;
}
您不必在$data=$this->input->post[“value”]中提及值键
第二件事呢
您编写了错误的语法$this->input->post[“value”]
写入语法是$this->input->post(“value”)
但是在$this->input->post(“value”);如果您无法获取值,因为没有具有值名称的键。首先,您可以通过
error:function(xhr){},
$.ajax({
type: 'POST',
url: '<?php echo site_url('customer_add/test'); ?>',
data: {
name: $('#name').val(),
phone: $('#phone').val(),
mail: $('#mail').val()
},
error: function(xhr) {
alert('error');
},
success: function(resp){
$('#error').html(resp);
}
});
希望对您有所帮助。尝试此
成功:函数(数据){//alert(“ok”);},错误:函数(er){alert(“有一些错误”);console.log(er.responseText);}
您正在请求ajax中的功能测试,在代码中,您正在向我们展示服务器脚本中的功能测试5???谢谢quarksera:)您是对的。我把它改为data:{value:value},现在我得到了响应,但问题是这样,表单验证不起作用。起初,它返回验证错误,但如果我写入数据,错误仍然是相同的,不会改变!您是否使用Codeignator表单验证或自定义?请给我一些提示,否则我将无能为力
$.ajax({
type: 'POST',
url: '<?php echo site_url('customer_add/test'); ?>',
data: {
name: $('#name').val(),
phone: $('#phone').val(),
mail: $('#mail').val()
},
error: function(xhr) {
alert('error');
},
success: function(resp){
$('#error').html(resp);
}
});
public function test5(){
$data['name'] = $this->input->post('name');
$data['phone'] = $this->input->post('phone');
$data['mail'] = $this->input->post('mail');
echo $data;
}