普通网友 2016-10-27 10:41
浏览 153

POST 500(内部服务器错误)Ajax

I want to send post request with ajax to controller in laravel. The ajax request send two input arguments and I want controller to find the column in the database with the first argument and then to set the name attribute with the second input argument. But I have this error message in console 500 (Internal Server Error).

Ajax function:

var $emailInput = $('input[name=eemail]').val();
var $finduser = $('[name=userName]').val();

$.ajax({
       type:"POST",
       url:'/code/task1/public/editUserAdmin',

       data: {
          'emailInput' : $emailInput,
           'finduser' : $finduser,
           },
          success:function(data){

       //  $("#editEmail").attr("readonly", true);
       //  $("#editEmail").val(data[0].name);

           alert("OK");
           }

   });

Route:

Route::post('/editUserAdmin', 'usersController@editUserAdmin');

Controller function:

$findUserInput = $request->input('finduser');
        $user = User::where('name',$findUserInput)->first();


         if(!$user){
            return response()->json(['status'=>false,'Description' => 'User could not be found.']);
        }
            //$user->name = $request->input('nameInput');
            $user->email = $request->input('emailInput');

            $user->save();
}

And also i import csrf everywhere because last time when I was making AJAX call i have problem with this csrf and the following code has fixed my problem, but now is not working.

<meta name="csrf-token" content="{{ csrf_token() }}">

 $.ajaxSetup({
                headers:{
                    'X-CSRF-TOKEN' : $('meta[name="csft-token"]').attr('content')
                }
            });  

        $.ajaxSetup({
                   headers: {
                    'X-CSRF-TOKEN': $('[name="_token"]').val()
                }
            });

and this 
<h3 class="media-heading" name="userName">{{ $user->name }}</h3>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" class="form-control paddingzero" class=text-center" readonly value="Name Name">
<input type="text" class="form-control paddingzero" class=text-center" name="eemail" id="editEmail" readonly value="{{ $user->email }}">

Any idea?

  • 写回答

1条回答 默认 最新

  • douzhushen_9776 2016-10-27 13:50
    关注

    Ajax does not have a "type" property. You will need to pass POST as method

    $.ajax({
       method: "POST",
       url:'/code/task1/public/editUserAdmin',
    
       data: {
          'emailInput' : $emailInput,
          'finduser' : $finduser,
       },
          success:function(data){
           alert("OK");
       }
    
    });
    

    If you are in dev mode you should enable error loggin / output. You can just open dev tools F12 (in chrome for example) and have a look at the error output. Proably it would be method not allowed or whatever.

    Another minor thing is that i would recommend to not prefix actual variables with $ if you do not reference the jquery object.

    var $emailInput = $('input[name=eemail]').val();
    var $finduser = $('[name=userName]').val();
    

    Instead do

    var $emailInput = $('input[name=eemail]'); // if you need it more than once
    var email = $emailInput.val();
    

    or if you only need it ones better name it

    var emailInput = $('input[name=eemail]').val();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题