Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
pass View through ajax
  • I'm am trying to update a div with a particular view using ajax.
    Namely, this is what I'm doing:

    $.ajax({
            url: 'v2test/devices',
            type: 'GET',
            data: { fleet_id: fleetId },
            dataType: 'html',
            success: function(output) {
                var content = document.getElementById('sidebar-content');
                content.innerHTML = output;      // update the inner html
            }
    });



    In my controller, I have the following:

        public function get_devices($fleet_id = null)
        {        
            $query = "SELECT `device_id` FROM `tmfleet_members` WHERE `tmfleet_id` = '$fleet_id'";
            $data['devices'] = DB::query($query)->as_object()->execute();
            $data['tmfleet_id'] = $fleet_id;
            $data['device_arr'] = Format::forge($data['devices'])->to_array();
            
            return View::forge('v2test/fleet/devices', $data); // The view has just <h2> hello </h2>
        }



    However, every time I make the request, the HTML of the current page I'm viewing (not the requested page) is outputed by ajax.

    Can anyone give me a hint as to what how I could do this?



  • HarroHarro
    Accepted Answer
    Is that controller a REST controller? Or a standard controller that adds the rest of the page?
  • it is a standard controller. 
    I just realized I had a mistake in the url, which I have fixed now.

    However, the question still stands: What is the best way for passing view to the client side so that a "one-page" application can be built?

    Also, granted I want to return multiple things, how can I put the view inside a json string?

  • HarroHarro
    Accepted Answer
    A REST controller doesn't have to return json, it can return HTML as well. It is just the default that is json.

    Check http://fuelphp.com/docs/general/controllers/rest.html#/config on which formats are supported out of the box, and how you configure it.

Howdy, Stranger!

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

In this Discussion