我有一个使用一点 Javascript 的 Symfony 应用程序。 Symfony 中有一个调用 API 的 ajax 请求:
$.ajax({
type: "POST",
url: "http://symfony.dev/api/price",
cache: false,
data: {
'volume': $('#tunnel_step_one_volume').val(),
'delivery': $('#tunnel_step_one_isStandardDelivery').val()
},
success: function (data, status) {
console.log(data);
Command.refreshData(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
})
现在 URL 已被硬编码。
我的问题是:
在 Symfony 中为 JS 文件指定路由名称的最佳实践是什么?
最佳答案
您可以使用https://github.com/FriendsOfSymfony/FOSJsRoutingBundle来实现这一目标。
最简单的方法是从 Controller 返回路由并在页面内的 js 变量中打印:
<script> myUrl = {{myUrl}} </script>
但我不认为这是最佳实践。
关于javascript - 在 Javascript 中使用 Symfony2 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981698/