laravel使用ajax发布请求

时间:2018-08-03 12:00:11

标签: laravel

这是我的路线

Route::post('/checkIfPasswordAvailable', 'PasswordAvailabilityController@password');

这是我的ajax

              function checkingIfPasswordExits(){
                    var password = document.getElementById('password').value;
                    var theToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
                    var theData = { "message": password, "token": theToken }
                    var xhttp = new XMLHttpRequest();
                    xhttp.open("POST", "/checkIfPasswordAvailable", true);
                    xhttp.onreadystatechange = function () {
                        if (this.readyState == 4 && this.status == 200) {
                            var data = JSON.parse(this.responseText).data;
                            console.log(data);
                            if(data.message == 'password is not available'){
                                 $('#warningMessage').show();
                                 return;
                            }

                            if (data.message == 'password is available') {
                                 $('.LandLords').submit();
                                 return;
                            }
                        }
                    };

                    xhttp.setRequestHeader('X-CSRF-TOKEN', theToken);
                    xhttp.setRequestHeader("X-Requested-With", 'XMLHttpRequest');
                    xhttp.setRequestHeader("Content-Type", 'application/json');
                    xhttp.send(JSON.stringify(theData));
                }

                checkingIfPasswordExits();

我一直在浏览器控制台上获取它 500内部服务器错误) 需要有人帮助我解决这个问题!

这是控制器

    <?php
    namespace App\Http\Controllers;
    use DB;
    use Storage;
    use Illuminate\Support\Facades\Request;
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Illuminate\Routing\Controller as BaseController;
    use Illuminate\Foundation\Validation\ValidatesRequests;
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

    class PasswordAvailabilityController extends BaseController
    {
     public function password(Request $request)
     {
           print_r($request->all());
           return;
     }
   }

那是我的控制器类

0 个答案:

没有答案