溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Laravel中怎么接收前端ajax傳來的數(shù)據(jù)

發(fā)布時間:2021-07-19 14:35:53 來源:億速云 閱讀:159 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了Laravel中怎么接收前端ajax傳來的數(shù)據(jù),內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

首先不得不感嘆vue的神奇,項目昨晚之后我會對整個項目中用到的技術(shù)和踩過的坑進(jìn)行一個總結(jié),今天先記錄一個前端傳送數(shù)據(jù)給后端,laravel接收的例子。

前端ajax插件我沒有使用vue-resource,說實話,用他遇到了坑,所以使用了axios.js,很好用,而且比vue-resource還小。

來看前端代碼(省略vue邏輯部分):

axios.post('index.php/login',{
    email:this.email,
    pass:this.pass
  }).then(function(res){
    console.log(res)
  }).then(function(){
    console.log(321)
  })

this.email和this.pass即為用戶填寫的表單數(shù)據(jù),點擊登錄即執(zhí)行這個方法(驗證數(shù)據(jù)格式?jīng)]問題后)。

來看Laravel如何接收這兩個值:

我們在app文件夾下建立一個GUser.php的Model文件,內(nèi)容如下:

<?php
namespace App;
use Mongodb;
use DB;
 
class GUser extends Mongodb {
  public static function login($email) {
 
    $mongo = DB::connection('mongodb');
    $res = $mongo->collection('user')->where('email',$email)->first();
    return $res;
 
  }
}

再在app/Http/Controllers下建立一個GUserController.php文件,內(nèi)容如下:

<?php
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\GUser;
use Illuminate\Http\Request;
 
class GUserController extends Controller{
 
  protected function login(Request $request) {
    $email = $request->input('email');
    $pass = $request->input('pass');
    $res = GUser::login($email);
    return $res;
  }
}

當(dāng)然啦,這里沒有用到pass的值,我在這里省略了登錄驗證的邏輯。

然后在路由文件web.php里配置:

Route::any(‘/login','GUserController@login');

上述內(nèi)容就是Laravel中怎么接收前端ajax傳來的數(shù)據(jù),你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI