普通网友 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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器