2016-02-12 58 views
0

我想产生一个Ajax请求的路径,但我得到了以下错误:如何在Symfony2中使用Routing.generate生成路由?

Error: The route "profesor_edit" requires the parameter "id". 

的代码如下:

$.ajax({ 
    type: 'POST', 
    url: Routing.generate('verify_user'), 
    data: {name:name}, 
    dataType: 'json', 
    success: function(response) { 

     if(response.data!=null){ 
      $("#container").load(Routing.generate('user_edit'), { id: response.data }); //"response.data" contains the value returned by the ajax call is an id value 
     } 
     else{ 
     $("#container").append("<span >It could not find the id</span>");   
     } 
    } 
    }) 

路由文件:

user_edit: 
    path:  /{id}/edit 
    defaults: { _controller: "BackendBundle:User:edit" } 

我想我会发送错误的参数,但这是我见过的唯一方法。我感谢您的帮助。

+1

如果使用FOSRoutingBundle,其'Routing.generate( 'user_edit', resonase.data)'[doku](https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/doc/index.md#generating-uris) – mador

+0

嗨@dokumador,我试过你的解决方案,现在我得到以下错误:'操作数e'中的'TypeError:invalid'。你知道这是为什么吗? – Joseph

+0

究竟是什么在'response.data'中?它必须是用户标识。 – mador

回答

2

假设你正在使用FOSJSRoutingBundle,使用以下命令:

$("#container").load(Routing.generate('user_edit', { id: response.data })); 
+0

嗨@chalasr,感谢您的回答,现在我已经来到相同的解决方案 – Joseph

+0

不客气@约瑟夫! – chalasr

1

最后我做到了这样:

$("#container").load(Routing.generate('user_edit', {id:response.data})); 
+0

这正是我所回答的。 – chalasr