Symfony路由ajax的get方法 [英] Symfony routing ajax get method
本文介绍了Symfony路由ajax的get方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过ajax GET参数传递,我的网址看起来像:
I pass by ajax GET parameters, my url looks like:
example.com/autocamplete?term=string
我为此添加了路线
autocomplete:
path: /autocomplete?term={term}
defaults: { _controller: CatalogWebBundle:Default:autocomplete }
但是我的路线有问题,因为出现错误:
But it something wrong with my route, because I get error:
GET http://127.0.0.1:8000/autocomplete?term=sdfsd 500 (Internal Server Error)
如何将我的路线重写为ajax get参数?
How to rewrite my route to ajax get patameters ?
JS是:
<script type="text/javascript">
$(function() {
//autocomplete
$("#search").autocomplete({
source: "\/autocomplete",
minLength: 1
});
});
</script>
推荐答案
安装 FOSJsRoutingBundle ,创建正确的路线,因为您的问题不正确.
Install FOSJsRoutingBundle, create a correct route as it is incorrect in your question.
因此,您应该执行以下操作:
So, you should do something like this:
添加捆绑包JS(可能在全局使用的基本模板中):
add bundle JS (may be in base template for global usage):
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
配置路由(无QS):
autocomplete:
path: /autocomplete
defaults:
_controller: CatalogWebBundle:Default:autocomple
options:
expose: true
配置您的JS:
<script type="text/javascript">
$(function() {
//autocomplete
$("#search").autocomplete({
source: Routing.generate('autocomplete', { 'term': value });,
minLength: 1
});
});
</script>
这篇关于Symfony路由ajax的get方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文