在Laravel 5.3中努力做一个基本的AJAX请求

时间:2017-01-28 19:33:12

标签: ajax laravel laravel-5.3

我正在尝试在Laravel中执行一个非常基本的AJAX请求,并且它一直给我500个内部服务器错误。我试图添加适当的标题,但仍然没有运气。谁能告诉我这里缺少什么?

我的路线:

Route::get('/checkpin', 'EmployeeLoginController@checkPin');

我的控制器:

<?php
    namespace App\Http\Controllers;

    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Http\Request;

    class EmployeeLoginController extends Controller {
        public function __construct() {
            $this->middleware('auth');
        }

        public function login(){
            $employees = DB::table('employees')->where([['clientID', '=', Auth::user()->userEmail]])->get();
            $adminEmployees = DB::table('employees')->where([['clientID', '=', Auth::user()->userEmail]]])->get();
            return view('auth/employee-login')->with(array('employees' => $employees, 'admins' => $adminEmployees));
        }

        public function checkPin($request) {

            if ($request->isMethod('post')){    
                return response()->json(['response' => 'This is post method']); 
            }

            return response()->json(['response' => 'This is get method']);
        }
    }

我的AJAX:

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

    $.ajax({
        type: "GET",
        url: "/checkpin",
        dataType: "json",
        data: "",
        success: function(response) {
            console.log(response);
        }
    });
});

如前所述,每当我点击按钮时,我的控制台都会出现500(内部服务器错误)错误。

有人知道我失踪的是什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我会尝试,首先尝试从路线的开头删除/:

Route::get('checkpin', 'EmployeeLoginController@checkPin');

并为您的函数添加类型转换

public function checkPin(Request $request) { ...

然后它不会被视为参数。