2016-09-18 82 views
0

我正在使用Laravel 5.2及其Auth scafolding。Laravel ajax登錄空字段

我試圖使默認登錄窗體,使用Ajax(無需重新加載頁面)。

我正在使用reqwest.js來處理ajax請求。

這裏是我的Ajax代碼:

<script data-cfasync="false" type="text/javascript" src="{{asset('js/lib/reqwest.min.js')}}"></script> 
<script type="text/javascript"> 
var thisForm = document.querySelector('#authLogin'); 
thisForm.addEventListener('submit', function(f){ 
    f.preventDefault(); 
    reqwest({ 
    url: thisForm.getAttribute('action'), 
    method: thisForm.getAttribute('method'), 
    contentType: 'application/json', 
    headers: { 
      'X-CSRF-TOKEN': document.getElementById('csrf_meta').getAttribute('content') 
    }, 
    data: '_token='+thisForm.elements._token.value+'&[email protected]&password=12345', 
    success:function (response){ 

    } 
    }); 
}); 
</script> 

它採用\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php login()函數來處理登錄請求:https://i.gyazo.com/bfe6790934f711f4c1d35f6670c20caa.png

這裏的問題:

1)當我使用默認的正常登錄表格/login(不含ajax),$request->all()在上面的類中返回提交的所有表單域(很好,這是完美的)

2),但是當我通過一個Ajax POST請求提交相同的形式/login$request->all()返回空數組:https://i.gyazo.com/44e2fc6f438bba27a2baab9f7d3b38d6.png

誰能告訴我在做什麼錯在這裏?我瘋了,不知道可能會出現什麼問題。我到處尋找解決辦法,仍然沒有。

謝謝。

回答

0

對不起,我很笨,我想通了它返回空數組的原因。

在我的ajax請求我有contentType: 'application/json',但我的表單數據不是JSON格式,但字符串參數格式。