2016-02-12 57 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