FOSJsRouting bundle returns 错误找不到“GET”的路由

FOSJsRouting bundle returns error No route found for "GET

我的树枝视图中有这个 table:

<table>
  <thead>
    <tr>
      <th>Data 1</th>
      <th>Data 2</th>
      <th>DETAILS</th>
      <th>EDIT</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {% for pc in arrayPointComptage %}
      <tr>
        <td>{{ pc.data1}}</td>
        <td>{{ pc.data2}}</td>
        <td>
          <a><button class="btn btn-info btn-xs showDetail" href="{{ path('detailsPointsComptage', {'id': pc.id }) }}">Detail</button></a>
          <a href="{{ path('editPointsComptage', {'id': pc.id }) }}"><button class="btn btn-warning btn-xs">Edit</button></a>
        </td>
      </tr>
    {% endfor %}
  </tbody>
</table>

<dialog id="window" title="detail pc"></dialog>

我正在使用 FOSJsRoutingBundle,以便通过 ajax 方法使用路由。

当我单击详细信息按钮时,它 returns 出现在一个对话框标签中,其中包含我需要的所有详细信息。

这是 javascript 代码,以便在对话框中返回详细信息 window:

// dialog window behavior
(function() {
    var dialog = document.getElementById('window');
    // Array of the buttons.
    var showButtons = document.getElementsByClassName('showDetail');
    // Event handler
    var showDialog = function() {
      // Now you have to use the show button as base , to find the data you want to display...
      console.log(this);
      dialog.show();
    };
    var i, len = showButtons.length;
    for(i = 0; i < len; ++i) {
        showButtons[i].onclick = showDialog;
    }
})();

//jax with FOSJsRouting
  $('.showDetail').click(function() {
    $.ajax({
      type: "GET",
      'url': Routing.generate('detailsPointsComptage', {'id': $(this).val()}),
      'success': function(loadDetail) {
          $('#window').html(loadDetail);
      }
    });
  });

根据文档,我在 app/config/config.yml:

中将我的路由公开为 true 参数
fos_js_routing:
    routes_to_expose: [ detailsPointsComptage ]

并确保在我的 routing.yml:

detailsPointsComptage:
    path:  /my/path/to/detail/{id}
    defaults: { _controller: MyBundle:MyController:detailsPointsComptage }
    requirements:
    methods: GET
    options:
        expose: true

当我点击详细信息按钮时,出现以下错误:

No route found for "GET /my/path/to/detail"

在浏览器中,右键单击我可以检查所有元素。在控制台选项卡上我有这个:

<button class=​"btn btn-info btn-xs showDetail" href=​"/​symfony_app/​web/​my/​path/to/​detail/​1">​Detail​</button>​ http://localhost/symfony/web/my/path/to/detail Failed to load resource: the server responded with a status of 404 (Not Found)

确实,如果我按照link http://localhost/symfony/web/my/path/to/detail,我有错误的找不到路线的GET,但如果我写这个url http://localhost/symfony/web/my/path/to/detail/1它returns我的观点是对的

我哪里错了?

您的路由需要 id 参数,这是必需的,所以这种行为是正常的。

问题是您的 $(this).val() 没有像您期望的那样 return id,您应该这样做,因为路由已经在 href 按钮的属性:

$('.showDetail').click(function() {
    $.ajax({
      type: "GET",
      'url': $(this).attr('href'),
      'success': function(loadDetail) {
          $('#window').html(loadDetail);
      }
    });
    return false;
  });

注意:将 href 用于 <button> 没有任何意义,您应该使用 <a>(或使用其他属性,例如 data-link):

<a class="btn btn-info btn-xs showDetail" href="{{ path('detailsPointsComptage', {'id': pc.id }) }}">Detail</a>