2012-01-19 26 views
3

我正在使用FOSJsRoutingBundle在JavaScript文件中生成Symfony 2路由網址。 我已經安裝束按照文檔(https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/tree/master/Resources/doc)FOSJsRoutingBundle:無法在JavaScript中生成路由網址

試圖執行表單提交使用AJAX腳本,其中,用戶能率和評論。

這裏是我的枝杈/ HTML代碼:

<form id="feedback" method="post" name="feedback"> 
    <div class="form">    
     <div class="form_layer"> 
      <div class="rating"> 
       <label>Rate Us:</label> 
       <input type="radio" name="rating" value="1"><small>One</small> 
       <input type="radio" name="rating" value="2"><small>Two</small> 
       <input type="radio" name="rating" value="3"><small>Three</small> 
       <input type="radio" name="rating" value="4"><small>Four</small> 
       <input type="radio" name="rating" value="5"><small>Five</small> 
      </div> 
     </div> 
     <div class="form_layer floated1"> 
      <div class="comment"> 
       <label>Please give your feedback</label> 
       <textarea rows="5" cols="32" id="comment" name="feedback"></textarea> 
      </div> 
      <div class="clr"></div> 
     </div> 
     <span class="comment_btn"><input type="submit" class="comment_submit" name="validate" value="Submit"></span> 
    </div> 
    </form> 

這裏是我的JavaScript代碼:

//Ajax Form Submission 
$(function() { 
    $('.comment_submit').click(function() { 
     var rating = $(':radio').val();    
     var comment = $('#comment').val(); 
     alert("comment: "+comment); 
     var route = Routing.generate('user_feedback'); 

     alert("Route: "+route); 

     /* $.ajax({ 
      type: "POST", 
      url: Routing.generate('user_feedback', { "rating": rating, "comment": comment}), 
      success: function() { 
      $('div.middle').html("<div class='message'></div>"); 
      $('.message').html("<h3>Thank You!</h3>") 
      .append("<p>Your comment has been submitted</p>") 
      .hide() 
      .fadeIn(1500); 
      } 
     }); 
     return false; */ 

     //alert("Rating"+comment); 
     //alert("Feedback Submited"); 
    }); 
}); 

我在我的routing.yml文件還

添加的路由現在的網址問題是當我提交表單時,我無法獲取路由值。我試圖警告你在代碼中看到的路由值,但我沒有得到vaue。當我提交時,表單只是刷新。

代碼有問題嗎? 我是Symfony 2的新手,請幫助我。

謝謝

回答

1

它不是一個symfony的問題,問題是在JavaScript

你有一個提交按鈕,這將提交表單。爲了防止表單提交,您必須按以下方式更改點擊功能:

$('.comment_submit').click(function(e) { 
    e.preventDefault(); 
    // your code goes here 
    return false; 
} 
9

檢查你的配置路線:

my_route_to_user_feedback: 
    pattern: /user/feedback 
    defaults: { _controller: HelloBundle:User:feedback } 
    options: 
     expose: true 

選項揭露必須是真實的。這對FOSJsRoutingBundle很重要!