sf-api-example/src/AppBundle/Resources/views/Category/index.html.twig
2016-11-23 21:00:23 +01:00

62 lines
1.4 KiB
Twig

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ form_row(form.name) }}
<pre id="response"></pre>
<input type="button" id="submit" value="Save">
<ul id="list">
</ul>
<script src="//codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ asset('bundles/fosjsrouting/js/fos_js_routes.js') }}"></script>
<script>
(function(w, $) {
var listCategories = function() {
$.ajax({
url: Routing.generate('get_categories'), // Si tes scripts sont dans un fichiers JS
}).done(function(collection) {
var $list = $('#list').html('');
collection.forEach(function(value) {
$list.append($('<li>').html(value.name));
});
});
}
$('#submit').click(function() {
var name = $('#name').val();
$.ajax({
url: Routing.generate('post_category_new'),
method: 'POST',
data: {
name: name,
}
}).done(function(response) {
$('#response').text(JSON.stringify(response));
listCategories();
}).fail(function(response) {
$('#response').text(JSON.stringify(response.responseJSON, null, 4));
listCategories();
});
});
listCategories();
})(window, jQuery);
</script>
</body>
</html>