Ajax/Angular post to SinglePage Controller

I was writing a small package for C5 today. In the dashboard I want to use a small Angular app instead off 10 single pages with controllers. I seem to have some trouble with posting data to my controller. Get requests work fine, but I can't seem to extract data in the controller which I pass along with the post requests.

I'm posting the following data to the controller:
var someObject = { start : 'DateTimeString' , end : 'DateTimeString' , max : 99};

POST request
return $http.post(
                someObject )

Controller method
public function saveData(){
        if ($this->isPost()){
            $data = $this->post();
            $val = Loader::helper('validation/form');
            $val->addRequired('start', t("Shit's required yo."));
            $val->addRequired("end", t("Shit's required yo."));
            $val->addRequired("max", t("Shit's required yo."));
            $error = $val->getError();
            if ($error->has()) {
                echo json_encode([
                    'success' => false,
                    'errors' => $error->getList()

I just need to be able to extract the data from the post request in order to validate and save it. I've been trying and failing for an hour now, so I hope someone can point out my mistake(s).

View Replies:
hutman replied on at Permalink Reply
If you put some echo statements in the controller are you getting into the correct function? Does it recognize that it came through a post? If you print_r the $data variable do you get the posted fields?
bleenders replied on at Permalink Reply
Yea, I was getting in the correct function. It was just that I could not get access to the data I put in to the POST request.
I found a way to get to it now, it's not pretty though. That's why I still think I'm doing something wrong or that there is a way to get to the data that I missed.

Normally with jQuery you would do something like:
$("#someForm").submit(function( event ){
   var $form = $( this ),
        var url = $form.attr( "action" );
        var posting = $.post(url,$form.serialize());
        posting.done(function( data ) {

In the controller method you would validate somewhat it like this:
$data = $this->post();
$val = Loader::helper('validation/form');
$val->addRequired('start', t("Some error message."));
$val->addRequired('end', t("Some error message."));
$val->addInteger('participants', t("Some error message."));

I expected it to work the same way when using

But for some reason I can't use
$data = $this->post()
Instead what I need to do to get the same result is
$data = (array)json_decode(file_get_contents("php://input"))

But as I said before, I suspect that there is an easier and more elegant way to do this. And I will find that way as soon as I have the time dive in to it a little deeper.

EDIT: I found this helpful http://stackoverflow.com/a/8893792...
hutman replied on at Permalink Reply
Is there any difference if you change it to be like this:

var serializedData = $.param({ start : 'DateTimeString' , end : 'DateTimeString' , max : 99});
    url: '/dashboard/controllerName/saveData',
    data: serializedData,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'