ForgejComponent
en

jComponent

Load data from a Total.js route with AJAX

jComponent 20 · jcomponent@20 jcomponentfrontendajaxrouting Executed with jComponent 20

AJAX('METHOD /url/', [data], callback) sends a request and calls the callback with the response. JSON responses are parsed for you. Combine it with SET() and any ui-bind or component bound to the path updates by itself.

controllers/api.js
exports.install = function() {
	ROUTE('GET /api/hello/', hello);
};

function hello($) {
	$.json({ message: 'Hello from Total.js' });
}
public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>

	<p><ui-bind path="greeting" config="text:value">Loading...</ui-bind></p>

	<script>
		AJAX('GET /api/hello/', function(response) {
			SET('greeting', response.message);
		});
	</script>

</body>
</html>
Try it in your browser

Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.

Preview