Laravel Ajax请求不能正常工作的控制器 [英] Laravel Ajax request not working of a controller

查看:151
本文介绍了Laravel Ajax请求不能正常工作的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel 4..2,并有一个HTML的onchange像图像功能。并显示我的错误作为第二个图像。

这AJAX请求不起作用上创建方法,但它的工作原理上laravel资源控制器的指数方法。

它的发生可能是因为laravel控制器。谁能帮我解释这一点。

  // routes.php文件

路线::资源(索引,索引控制器);

    路线::得到('AJAX-subcat',函数(){
        $ cat_id =输入::获得(cat_id);
        $子类别=子目录::其中('PARENT_ID','=',$ cat_id) - >获得();
        返回响应:: JSON($亚类);
    });

// AJAX

  <脚本>
          $('#类)。在('改变',功能(五){
              执行console.log(E);

              VAR cat_id = e.target.value;

              // AJAX
              $获得('AJAX-subcat?cat_id ='+ cat_id,功能(数据){
                       $('#子类别)空();
                       $每个(数据,函数(指数,subcatObj){
                      $('#子类别)追加('<期权价值=+ subcatObj.id +'>+ subcatObj.name +'< /选项>')

                           });

                      //的console.log(数据);


                      });
              });

< / SCRIPT>
 

解决方案

由于您使用Laravel,让我们开始使用laravel发挥到极致:

在routes.php文件,请创建:

 路线::得到('AJAX-subcat / {ID}','SOME_CONTROLLER_NAME @ SOME_METHOD_NAME');
 

在{ID}告诉laravel存储的任何'之后的细节/在一个变量,并把它传递到指定的控制器的方法。没有什么比拥有更丑?AJAX-subcat =车的URL。另外,请用正确的名称替换SOME_METHOD_NAME和SOME_CONTROLLER_NAME。

在您的控制器,请加:

 公共职能THAT_METHOD_NAME_FROM_THE_ROUTE($ ID){
  $子类别=子目录::其中('PARENT_ID','=',$ ID) - >获得();
  返回响应:: JSON($亚类);
}
 

而在你的Ajax脚本

 <脚本>
      $('#类)。在('改变',功能(五){
          执行console.log(E);

          VAR cat_id = e.target.value;

          // AJAX
          $获得('AJAX-subcat /+ cat_id,功能(数据){
                   $('#子类别)空();
                   $每个(数据,函数(指数,subcatObj){
                  $('#子类别)追加('<期权价值=+ subcatObj.id +'>+ subcatObj.name +'< /选项>')

                       });

                  //的console.log(数据);


                  });
          });

 < / SCRIPT>
 

I am using laravel 4..2 and have a html onchange function like the image. And showing me the error as the second image.

This AJAX request does not work on create method but it works on index method of a laravel resource controller.

Its happening may be because of laravel controller. Can anybody help me by explaining this.

// Routes.php

Route::resource('index', 'IndexController');

    Route::get('ajax-subcat', function(){
        $cat_id = Input::get('cat_id');
        $subcategories = Subcategory::where('parent_ID', '=', $cat_id)->get();
        return Response::json($subcategories);
    });

// AJAX 

  <script>
          $('#category').on('change', function(e){
              console.log(e);

              var cat_id = e.target.value;

              // AJAX
              $.get('ajax-subcat?cat_id=' + cat_id, function(data){
                       $('#subcategory').empty();
                       $.each(data, function(index, subcatObj){
                      $('#subcategory').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>')

                           });

                      //  console.log(data);


                      });
              });

</script>

解决方案

Since your using Laravel, lets begin using laravel to its fullest:

In Routes.php, please create:

 Route::get('ajax-subcat/{id}', 'SOME_CONTROLLER_NAME@SOME_METHOD_NAME');

the "{id}" tells laravel to store the details of whatever comes after the "/" in a variable and pass it on to the specified controllers method. There is nothing more ugly than having ?ajax-subcat=cars in a URL. Also please replace SOME_METHOD_NAME and SOME_CONTROLLER_NAME with the correct names.

In your Controller, please add:

public function THAT_METHOD_NAME_FROM_THE_ROUTE($id){
  $subcategories = Subcategory::where('parent_ID', '=', $id)->get();
  return Response::json($subcategories);
}

And in your Ajax Script

   <script>
      $('#category').on('change', function(e){
          console.log(e);

          var cat_id = e.target.value;

          // AJAX
          $.get('ajax-subcat/' + cat_id, function(data){
                   $('#subcategory').empty();
                   $.each(data, function(index, subcatObj){
                  $('#subcategory').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>')

                       });

                  //  console.log(data);


                  });
          });

 </script>

这篇关于Laravel Ajax请求不能正常工作的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆