Ajax/Angular post to SinglePage Controller

Permalink
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(
                '/dashboard/controllerName/saveData',
                someObject )
                .then(onComplete)
                .catch(onFailed);


Controller method
public function saveData(){
        if ($this->isPost()){
            $data = $this->post();
            $val = Loader::helper('validation/form');
            $val->setData($data);
            $val->addRequired('start', t("Shit's required yo."));
            $val->addRequired("end", t("Shit's required yo."));
            $val->addRequired("max", t("Shit's required yo."));
            $val->test();
            $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).

bleenders
 
hutman replied on at Permalink Reply
hutman
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
bleenders
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 ){
   event.preventDefault();
   var $form = $( this ),
        var url = $form.attr( "action" );
        var posting = $.post(url,$form.serialize());
        posting.done(function( data ) {
        ///RESULT
        });
});


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


I expected it to work the same way when using
$http.post


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
hutman
Is there any difference if you change it to be like this:

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