FOSJsRouting捆绑返回错误找不到" GET

时间:2015-09-24 08:50:02

标签: javascript ajax symfony twig url-routing

我的树枝视图中有这张表:

<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方法的路径。

当我点击详细信息按钮时,它会返回一个对话框标签,其中包含我需要的所有细节。

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

// 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

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

  

找不到&#34; GET / my / path / to / detail&#34;

的路线

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

  

<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无法加载   资源:服务器响应状态为404(未找到)

实际上,如果我按照http://localhost/symfony/web/my/path/to/detail链接,我会发现GET找不到路由错误,但如果我写了这个网址http://localhost/symfony/web/my/path/to/detail/1,则会返回正确的视图。

我哪里错了?

1 个答案:

答案 0 :(得分:1)

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

问题是你的$(this).val()没有按预期返回id,你应该这样做,因为路由已经在href属性中生成了你的按钮:

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

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

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