Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax NotFoundHttpException:未找到“的路由”;邮寄/邮寄SYmfony2_Ajax_Symfony - Fatal编程技术网

Ajax NotFoundHttpException:未找到“的路由”;邮寄/邮寄SYmfony2

Ajax NotFoundHttpException:未找到“的路由”;邮寄/邮寄SYmfony2,ajax,symfony,Ajax,Symfony,我有一个ajax发布的表单,这让我想扯头发,因为我尝试了基于几乎类似问题的各种答案,但都没有用 我的routing.yml文件中有以下路由 _save_profile: pattern: /register/save-profile/{data} defaults: {_controller: MYBundle:Registration:saveProfile} requirements: _method: GET|POST options: expose: true

我有一个ajax发布的表单,这让我想扯头发,因为我尝试了基于几乎类似问题的各种答案,但都没有用

我的routing.yml文件中有以下路由

_save_profile:
pattern:    /register/save-profile/{data}
defaults: {_controller: MYBundle:Registration:saveProfile}
requirements:
    _method:  GET|POST
options:
    expose: true   
并使用以下代码发布我的表单

var postData = $('#form').serializeArray();
$.ajax(
      {
     url: Routing.generate('_save_profile',{
         type: "POST",
         data : postData,
         }).done(function()
         {
            alert("Saved");
         });

任何帮助都将不胜感激

您不需要通过route中的参数
{data}
发送表单数据。如果您想用ajax发送表单,那么您需要

更改路线:

_save_profile:
    pattern:    /register/save-profile/
    defaults: {_controller: MYBundle:Registration:saveProfile}  
var postData = $('#form').serializeArray();

    $.ajax({
      url: Routing.generate('_save_profile'),
      type: "POST",
      data: postData,
      dataType: "json",
      success:
              function(result) {

                    console.log(result);

              },
      error:
              function() {
               alert('Error')
              }
    });
更改js:

_save_profile:
    pattern:    /register/save-profile/
    defaults: {_controller: MYBundle:Registration:saveProfile}  
var postData = $('#form').serializeArray();

    $.ajax({
      url: Routing.generate('_save_profile'),
      type: "POST",
      data: postData,
      dataType: "json",
      success:
              function(result) {

                    console.log(result);

              },
      error:
              function() {
               alert('Error')
              }
    });
注意:我不使用
FOSJsRoutingBundle
捆绑包进行js路由。我总是在“数据”属性中的模板上渲染路线。比如说

html

 <form type="POST" id="form" data-url="path('_save_profile')">
参考资料

var url = $('#form').data('url');

  • 为什么不省略
    \u方法
    要求并接受所有方法?您使用的是什么版本的Symfony?在2.2版本中,语法发生了变化,更近一点看,似乎您只是输入了一个错误。它不应该是
    url:Routing.generate(“\u save\u profile”),键入:'POST',…
    ?你的代码实际上应该会在浏览器控制台中触发一个JS错误。我使用的是symfony 2.4版,没有javascript错误,引用的url工作正常。最初,我没有使用_方法,而是将其添加到解决方案中,我一直在选择所有过度情绪化的_方法,但仍然没有改变,非常感谢!工作得很有魅力!