Instance sharing laravel receive front-end AJAX-transmitted data

Source: Internet
Author: User
Recently in making a note of the project, the technology stack is as follows: Vue.js + Laravel + MongoDB first had to sigh the magic of Vue, project after last night I will be on the whole project to use the technology and trampled the pit to make a summary, today first record a front-end transfer data to the backend, Examples of Laravel received.

Front-end Ajax plug-in I did not use Vue-resource, to tell the truth, with him met the pit, so used the axios.js, very easy to use, but also smaller than Vue-resource.

Look at the front-end code (omitting the Vue logic section):


Axios.post (' Index.php/login ', {    email:this.email,    pass:this.pass  }). Then (function (res) {    Console.log (res)  }). Then (function () {    console.log (321)  })

This.email and This.pass are the form data that the user fills out, click Login to execute this method (verify that the data format is not a problem).

See how Laravel receives these two values:

We create a guser.php model file under the App folder, which reads as follows:


<?phpnamespace 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;   }}

Then create a gusercontroller.php file under App/http/controllers, with the following content:


<?phpnamespace 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;  }}

Of course, there is no pass value here, I omitted the logic of login verification here.

Then configure it in the routing file web.php:


route::any ('/login ', ' gusercontroller@login '); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.