Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to save JSON?
  • Hi,

    I'm quite new to all whats Json... So let me explain what the purpose is:

    I have a simple Template Controller.
    On the homepage I have 1 div which will generate a canvas using a javascript library.
    With that javascript library I can simply call toJson() to create a json String.

    Now how do I get that Json string to a controller?
  • What do you mean by "to a controller"? Javascript is client-side, Controllers are server-side.

    You want your page to do an AJAX call and post some JSON data?
  • I understand that javascript is client side... But I suppose there is a way to get that JSON string and save it to a database.
  • Create a REST method, either in a separate controller using Controller_Rest, or in your existing controller by switching to Controller_Hybrid.

    Create a method called 'post_yourname' (instead of 'action_yourname'), do the usual security checks, and after post you can use Input::post like with a normal form.

    On the client side, there are several ways to do API calls. For example JQuery's AJAX calls.
  • Thanks for the response, and sorry for my late reply.

    But it seems that when doing this ajax call with JQuery you need to put an 'url' to which the request is sent.

    Should it be used?
    If using an url is it enough to put the URI method or the full url?
  • Executing Ajax calls using JQuery is a bit out of scope for me, perhaps someone else can help you further.
  • K, so this is what I have as of now:


    But it doesn't do anything. 
  • Have you tried to do some debug? 
    Put a Log::error(); after every line in the ost_save method, you you can understand where it stops.
  • Well I tried a few things: and finally edited my save function to the following:


    Now this gives me the error message. But no specific one (simply "object Object" and the text I'm sending).
    To be sure I checked whether the json text was valid and it is. So no issues there.

    So it might be related to the url. As of now I tried sending to the direct url of the home controller and the uri. None worked.


    EDIT:
    @ilNotturno: forgot to tell... it doesn't get to the Log:error() point yet.
  • K so when I remove the url attribute and leave it as a default (which is the current url) it gives me the following error in return:

    ErrorException [ Warning ]: Attempt to assign property of non-object
    (on the template object: title and content).

    Is there maybe an issue with the Hybrid controller?

    EDIT: it seems when not using an url it uses the index function and not the post_save.
    Another person seem to be having the same issue: http://fuelphp.com/forums/discussion/10896
  • To keep all posts readable, I'm craeting this new reply.

    I FINALLY GOT IT TO WORK!!!!! YEEeeey

    So it seemed the url attribute was wrong.
    Just to be sure and to keep teh ajax calls apart I created a new ajax Controller:

    As you can see the url is just the controller and method instead of the full url/uri.
    This worked!

    For some reason the Log doesn't work but writing to a file did.

    Regards

Howdy, Stranger!

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

In this Discussion