简体   繁体   English

如何在控制器中获取JSON发布数据

[英]how to get json posted data in the controller

I'm working on a CodeIgniter project and I'm newbie to ajax/jquery. 我正在研究CodeIgniter项目,并且是ajax / jquery的新手。 I'm using fullCalendar. 我正在使用fullCalendar。 In my my view I want to post the date of an event and then post it to the controller. 在我看来,我想发布事件的日期,然后将其发布到控制器。 Here is my CalendarView.php 这是我的CalendarView.php

$(document).ready(function() {
    $('#calendar').fullCalendar({
        dayClick: function(date, allDay, jsEvent, view) {  
               add_event(date);
        },
        events: 'https://www.google.com/calendar/feeds/myLogin%40gmail.com/public/basic'
    });
});

//Ajax call
function add_event(date) { 
    $.ajax({
            type: 'POST',
            url: '<?php echo site_url()."/welcome/add_event/"; ?>',
                        data:{ eventsJson: JSON.stringify(date) },
                        dataType : "json",
            success: function (response) {
                alert(response);
            }
    });
}​

In my controller, here is what I do: 在我的控制器中,这是我的工作:

$date= $this->input->post('data');

Could anyone help me ? 有人可以帮我吗?

I don't know much about codelgnitor, but the data arrives in object form, you can retrieve the date string as $_POST['data']['eventsJson'] 我对codelgnitor不太了解,但是数据以对象形式到达,您可以将日期字符串检索为$ _POST ['data'] ['eventsJson']

Though I don't think "eventsJson" is a very intuitive name, probably should be called "date" instead ;-) 尽管我不认为“ eventsJson”是一个非常直观的名称,但应该将其称为“ date” ;-)

In the controller to receive the eventsJSON you'd do: 在接收eventsJSON的控制器中,您可以执行以下操作:

$eventsJSON = json_decode($this->input->post('eventsJSON'), TRUE);

Now $eventsJSON contains an array with your json data. 现在$eventsJSON包含一个包含您的json数据的数组。 However, if you're only sending back the date you don't need to pass it back as JSON, plus eventsJSON is a bad name for it. 但是,如果您只发送回日期,则不需要将其作为JSON传递回去,再加上eventsJSON对于它eventsJSON是一个不好的名字。 In this case you could simply change the data part of the ajax call like: 在这种情况下,您可以简单地更改ajax调用的数据部分,例如:

data: 'date='+date,

And in your controller: 在您的控制器中:

$date = $this->input->post('date');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM