jquery.post()值总是失败

时间:2017-12-26 06:35:47

标签: jquery post laravel-5

我是laravel的新手,所以请考虑一下。当我使用jquery.post方法时,我总是得到结果失败,我不知道为什么这是路线的问题或不在这里是我的代码请看看

<script>
$(".fa-exchange").click(function(){
  var doctor_id=$(this).attr('doctorid');
  $("#doctor_id").val(doctor_id);
  var status=$(this).attr('status');
  $("#status").val(status);
        });
  $("#change-ok").click(function(){

            var doctor_id=$("#doctor_id").val();
            var status=$("#status").val();
            var url='{{url('/change-doctor-status/')}}';

            $.post(url, {doctor_id:doctor_id,status:status }).done(function (result) {
               alert("success");
            }).fail(function(result) {
              alert("There was an error. Try again please!");

        });

        });
</script>

我的控制器看起来像这样

public function change_doctor_status(Request $request)
{


   $id = $request['doctor_id'];
   $status = $request['status'];
   if($status==1)
   {
      $query=DB::table('doctors')->where('id',$id)->update(['status' => 0]);
      if($query)
      {
        return true;
      }

   }
   else if($status==0)
   {
     $query=DB::table('doctors')->where('id',$id)->update(['status' => 1]);
      if($query)
      {
        return true;
      }
   }
}

我这样做了

Route::post('/change-doctor-status', 'Admin\AdminController@change_doctor_status');

在这里,当我找到结果时,我总是得到这样的警报(&#34;出现了错误。请再试一次!&#34;);它存储在失败部分

1 个答案:

答案 0 :(得分:1)

如果您在刀片文件中写这个

$.post(url, {doctor_id:doctor_id,status:status }).done(function (result) {
               alert("success");
            }).fail(function(result) {
              alert("There was an error. Try again please!");

再添加一行。

$.post(url, {doctor_id:doctor_id,status:status,_token:'{{ csrf_token() }}' }).done(function (result) {
               alert("success");
            }).fail(function(result) {
              alert("There was an error. Try again please!");