du1108 2014-02-05 16:24
浏览 61

Laravel使用$ .post发布数据

I'm trying to set up ajax for laravel, but it won't work. I have controller for check validation and update from database. javascript which blocks the submit form do not work and page is refresh.

do not work means: alert('SUBMIT'); into submit form not work and alert me without any error and warning.

i test alert('SUBMIT'); but that does not work. but alert('ACTION'); correct and alert me.

my included correct JS:

<head>
    <title>Alachiq</title>
    <meta charset='UTF-8' />

    {{HTML::script('js/jquery-1.9.1.min.js')}}

</head>

jQuery:

<script type="text/javascript">
    $(document).ready(function() {

       alert('ACTION');//

       $('#submit').click(function(){

       alert('SUBMIT');

       name         = $('#name').val();
       family       = $('#family').val();
       email        = $('#email').val();
       currPassword = $('#currPassword').val();
       password     = $('#password').val();
       password_confirmation = $('#password_confirmation').val();     

       $.post("{{ URL::route('admin.profile.update') }}",
            { 
              name                  : name,
              family                : family,
              email                 : email,
              currPassword          : currPassword,
              password              : password,
              password_confirmation : password_confirmation  
            },
            function()
            {
                alert('sss');
            },'json');
       });
});
</script>

my sample of form:

{{ Form::model($profile, array('route' => array('admin.profile.update', $profile->id), 'method' => 'PUT')) }}

...

{{ Form::close() }}

my simple controller

public function update($id)
{
  if ( Request::ajax() ){
             return Response::json(array(
                    'errors'=>'ddddd'
                ));
  }
}

UPDATE: my route:

Route::group(array('before' => 'auth'), function()
{

  Route::get  ('admin/contents', array('as'=>'contents', function(){
      return View::make('back_end.layouts.contents');
  }));
  Route::resource  ('admin/profile' , 'ProfileController'          , array('as'=>'profile', 'before'=>'csrf'));

  Route::get('admin/createNewContents' ,array('as'=>'createNew', function(){
      return View::make('back_end.layouts.createNewContents');
  }));
});
  • 写回答

1条回答 默认 最新

  • duangong1979 2014-02-05 17:03
    关注

    Try this

    {{ Form::model($profile, array('route' => array('admin.profile.update', $profile->id), 'method' => 'PUT', 'id' => 'frm')) }}
    
    {{ Form::close() }}
    

    Notice the 'id' => 'frm' in the form model after 'method' => 'PUT'. Now use following code to submit the form

    $('#frm').submit(function(e){
        e.preventDefault();
        // rest of your code
    });
    

    Update: Change the route declaration to this (Also add _method:"PUT" in your post data):

    Route::group(array('prefix'=> 'admin', 'before' => 'csrf'), function(){

    Route::resource('/profile' , 'ProfileController');
    

    });

    Now from command prompt/terminal run php artisan routes command and check each routes (url and name) to find out what is the right route for this action Laravel used.

    Update: Add this in your ProfileController controller

    public function __construct()
    {
        $this->beforeFilter('csrf', array('on' => array('post', 'delete', 'put', 'patch')));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值