dongwuwei0718 2013-08-13 04:00
浏览 229

如何在codeigniter控制器中接收ajax json数据

On the client side I have :

var postData = {
    "id" : id,
    "message" : message
};

console.log(postData);

$.ajax({
    type: "POST",
    url: "controller/function",
    data: postData, 
    success: function(){
        alert(id + ' ' + message);
    }
});

This appears to be working properly as I can see the correct post parameters in chrome dev tools. In my codeigniter controller I have tried:

echo 'postid' . $_POST['id'].' '.$_POST['message'];

$postData=$this->input->post('id');

var_dump($postData); exit;

I'm getting:

Message: Undefined index: id
Message: Undefined index: message

boolean(false)

the $_POST array is empty.

How can I fix this? Thank you for your help

  • 写回答

4条回答 默认 最新

  • dongruo0909 2013-08-13 04:05
    关注

    you may add dataType:'json' in your ajax options

    $.ajax({
           type: "POST",
           url: "controller/function",
           data: postData,
           dataType:'json',
           success: function(){
               alert(id + ' ' + message);
           }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码