Laravel How to use Ajax

Source: Internet
Author: User

    1. HTML Blade Section

      <meta name= "_token" content= "{{Csrf_token ()}}"/>
    2. Front-end JS Request section (Note that the header attribute is written to avoid cross-site forgery request attacks)

      $.ajax ({type: ' POST ', url: '/ajax/create ', data: {date: ' 2015-03-12 '},datatype: ' json ', headers: {' X-csrf-token ': $ (' Meta[name= "_token"). attr (' content ')},success:function (data) {Console.log (data.status);},error:function (XHR), Type) {alert (' Ajax error! ')}});
    3. Route section route.php (ajax/create route is processed on controllers/ajax/pollcontroller.php store method)


Route::group ([' prefix ' = ' ajax ', ' namespace ' = ' ajax '], function () {Route::p ost (' Create ', ' [email protected] ');});


    1. Controller Method Pollcontroller.php,


<?php namespace app\http\controllers\ajax;use app\http\requests;use app\http\ controllers\controller;use illuminate\http\request;use illuminate\http\response;use app\poll; Data Model use redirect, input, auth, log;class pollcontroller  for   //  Extends controller {public function store (request  $request) {    $ poll = new poll;     $poll->date = input::get (' Date ');     if  ($poll->save ())  {        return  response ()->json (             ' Status '  => 1             ' msg '  = >  ' OK ',        );     } else {          return redirect::back ()->withinput ()->witherrors (' Save failed! ');     }}


Laravel How to use Ajax

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.