Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Incorrect output from REST Controller
  • Hi, I'm using FuelPHP 1.5.2 and I made a simple function that gets a random sentence from an array...

    I made a Controller_Rest that contains a function called fsbp_rand() with the following code:

    shuffle($this->_fsbp_list);
    $response = array("result" => $this->_fsbp_list[0]);
    return Response::forge((array) $response);

    I also have an array called $_fsbp_list (private) containing the sentences.

    When I make an AJAX request with jQuery:


    $.ajax({
    type : "POST",
    url : "http://www.example.com/subfolder_here/public/index.php?/api_rest/fsbp_rand",
    dataType : "json",
    });
    I get this text (mimetype: text/html) as result:

    array(1) { ["result"]=> string(61) "SENTENCE GOES HERE" }


    In my config I'm ignoring the ACCEPT header, already tried without ignoring it.
    No authentication involved, it will be a public api.

    The Rest controller worked fine with other functions. So this is the only case
    I'm sure that the array json_encodes correctly because I tried the whole function code in Oil's console... Don't know what's wrong! :(
  • The jQuery ajax call. You either need to pass the accept type (and not ignore the accept header) to fix it client side, or force the controller to return json server side by setting the 'format' property to 'json'.

  • Well, I tried client-side then server-side fix. Even both at the same time! But it still doesn't work...
  • HarroHarro
    Accepted Answer
    I just checked one of my apps, and I don't do anything special.

    On the server side it's a normal controller that extends Controller_Rest, all methods in that controller return an array.

    On the client side, the ajax call looks like this:
                        $.ajax({
                            url: select.attr('source'),
                            type: "GET",
                            dataType: "json",
                            data: {
                                query: request.term,
                                linkfilter: select.attr('linkfilter')+':'+$('#'+select.attr('linkfield')).val()
                            },
                            success: function(data) {
                                response($.map(data, function(item) {
                                    return {
                                        label: item.value,
                                        value: item.key
                                    }
                                }));
                            },
                        })
    And this works without problems.
  • Thank you, that worked perfectly... But to save time, not making an AJAX request: I made it all into javascript...

Howdy, Stranger!

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

In this Discussion