doujiu8826 2015-01-05 09:18
浏览 39
已采纳

发送ajax帖子时没有在php codeigniter中接收数据

I'm trying to send data by post using ajax (with codeigniter) and I don't know why but I don't receive anything...

This is how I send it:

var sendData = $('#formContact').serialize();
$.ajax({
    type: 'POST',
    url: '<?php echo base_url()?>/intranet/update/updateProfile',
    data: sendData,
    dataType: 'json',
    success: function (data) 
    {
        console.log(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

and this is an example of my form:

<form id="formContact" action="update" method="POST">
    <input class="headInput" type="text" name="userName" value="Tito"/>
    <input class="headInput" type="text" name="userLastName" value="Lancreo"/>
    <input class="headInput" type="text" name="phone[]" value="666666"/>
    <input class="headInput" type="text" name="phone[]" value="111111"/>
    <input class="headInput" type="text" name="phone[]" value="222222"/>
</form>

And when I debug it, I always get 0...

[false, false, Array[0], false, null]

My controller:

$this->load->helper('form');
$this->load->library('form_validation');

//1 way
$ret=$this->input->post();

//2 way        
$return=$this->input->post(NULL, TRUE);

//3 way
$all=$_POST;
json_encode($all);

//4 way
$contact=$this->input->post("userName");

//return everything...
$var[0]=$return;
$var[1]=$contact;
$var[2]=$all;
$var[3]=$ret;
$var[4]=$data;
echo json_encode($var);

How can I fix it??

  • 写回答

2条回答 默认 最新

  • doujishan2247 2015-01-08 07:32
    关注

    SOLVED!

    The problem was not to replace with:

    serialize().replace(/%5B%5D/g, '[]');
    

    But I think it's usefull...

    My problem was that I'm using a library for internationalization (https://github.com/bcit-ci/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18n) and I must add language to my url, even if I change my routes.php

    url: '<?php echo base_url()?>en/intranet/update/updateProfile'
    

    Thanks a lot!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?