Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
handeling json input::post in a rest controller ...
  • Hi everybody .. What I would like is to use the data and save it into my database ..
    shouldn't be that difficult but I don't seem to get it right .. Very new at everything so i would very much appreciate some tips ..
    I don't quite understand how I can get this to work ..
    any thoughts what I miss here ? This is the data i get from my view
    generated by knockout.js
    $.ajax({
         // saving data to rest controller 
         url : "/q/auth/savethis",
         type: "post",
         data: ko.toJSON(ViewModel), 
         contentType: "application/json", 
         success: function (result)
          alert(result.message+" "+result.firstName+" "+result.lastName) ;
         }
    

    Which generates me this data to send to my controller: {"userName":"Username","userPass":"","userEmail":"Email@address.adr","firstName":"FirstName","lastName":"LastName","fullName":"FirstName LastName"} */ scarap at : http://scrp.at/ach
     public function post_savethis()
     {
       $firstName  = \Input::post("firstName"); 
       $lastName  = \Input::post("lastName");    
       
       // but first just return some data back to the view .. 
       $this->response(array(
        'valid'  => true, 
        'message'  => 'Did we got any data ? ', 
        'firstName' => $firstName ,  
        'lastName' => $lastName   
        ), 
     } /* end of savethis */
    

    Ok solved this :
    data: JSON.parse(ko.toJSON(ViewModel)) ,
    ko.toJSON Get's me the ViewModel in JSON format and JSON.pasrse get's it to play nicely with js / jquery
  • Looks like you are encoding to JSON then decoding again just to make an array. Can't you just send an array?
  • Phil Sturgeon wrote on Thursday 11th of August 2011:
    Looks like you are encoding to JSON then decoding again just to make an array. Can't you just send an array?

    Hi .. thanks a lot f taking the time to read this .. Yeah seems that way .. the ko.toJSON gives me a plain json string .. i had expected that I could pass the json string directly to my rest controller .. when i try to access the sent data in my controller .. just seems empty.. with this construction seems to work but i am sure there is a simpler way but i can't seem to get it .. what do you mean sending just an array?
    because knockout has those observables i have to use (t)his ko.toJSON function don't see any other way .. it works .. so maybe later will fix with something else.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion