Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
JSON output not correct using Controller_Rest with post_* function
  • Hey guys!

    I'm new to FuelPHP, and am trying to convert an app from CodeIgniter. The Rest Controller seemed like a neat trick to making my api's go a little quicker.

    Long story short, I've tried to rebuild my login funcitons using Controller_Rest, but I keep getting the wrong output.

    The output I'm getting is prefixed with the word Object, making me think that there's a problem, possibly with the to_string of my array.

    I'm returning a response with just:
    return $this->response(array(
    'test' => 'This is a test.'
    ));

    Guess I'm just not sure what I'm doing wrong to confuse it. Any help would be greatly appreciated! Thanks!

  • response() will just do a json_encode() of the array passed (assuming it has to return json, so if that is configured correctly), and return that as-is. I use it on a daily basis.

    Have you checked (with for example Firebug) what is actually requested and send back?
  • First, thanks for the help!

    I
    've got the JS logging the output to the console, and it looks like I'm getting this: 
    Object {test: "This is a test."}

    It looks like it's doing the json_encode(), but then it's spitting out the object, instead of the raw JSON. Not really sure what would cause it to do that.

    It's an issue because when the js is trying to parse through it, it sees that dang O, and fails.
  • Update:

    When I run the output array through json_encode(), then pass that as an argument to response(), it sees it as a string, and skips running Format::to_json(). THAT looks correct.

    So, it appears that the Format::to_json() is not working correctly. I've got a way to work around that, but I'm not really happy with a work around for this.
  • HarroHarro
    Accepted Answer
    That's odd, because I use that on a daily basis.

    As you'll see in Format::to_json(), it does nothing but a json_encode(). It even explicitly converts an object to an array before encoding it.

    There is a note in the PHP json_encode() comments about returning an object when an indexed array doesn't have consecutive keys, but I haven't observed that either, as I use REST calls to populate dropdowns, and id's in the table are seldom consecutive.
  • It's very possible it's something with my setup, but at this point, I feel the need to continue working, rather than fight with something that I have a work around for.

    If there's some way I would be able to submit a bug report, as well as any needed information, I'd be more than willing to do that.

    I just think I was more upset about grinding my gears for a whole day on that problem rather than get any work done.

Howdy, Stranger!

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

In this Discussion