Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
pass View through ajax
JeanAlesi
January 2016
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?
Harro
January 2016
Accepted Answer
Is that controller a REST controller? Or a standard controller that adds the rest of the page?
JeanAlesi
January 2016
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?
Harro
January 2016
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.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
January 2016
JeanAlesi
January 2016