Laravel 5.2 valid ajax request

As we can check in Laravel 5.2, if the request is a valid ajax request. In codeigniter, we could check this out as $ this-> input-> is_ajax_request (). Does Laravel 5.2 have something similar?

Also, I would like to know how we can check the request for the csrf token. Is it fine if I let my webpage render through the "web" means by creating a csrf token and then passing that token as an ajax request parameter? Will Laravel take care of checking the token or is there an alternative way to do this?

I checked the laravel 5.2 documentation, and since this is the first time I'm dealing with the laravel framework, it looks like the documentation assumes that the reader is already familiar with earlier versions of the framework. For a novice like me this is a bit overwhelming.

Thanks in advance. Please let me know if you need additional materials.

Prakhar

0
source share
4 answers

I think this may help you discover the easiest way to use AJAX with Laravel.

This is really an old piece of code, but it works jajajaja

Controller side:

/**
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function getRamos(Request $request)
{
    $check = Ramo::find($request->input('ramo'));
    $subramos = Subramo::where('ramo_id', $check->id)->get(['nombre_subramo']);
    if($request->ajax()){
        return response()->json([
            'subramos' => $subramos
        ]);
    }
}

Ahead:

<script>
    $(document).ready(function(){
        $('#ramo').change(function(){
            var ramo, token, url, data;
            token = $('input[name=_token]').val();
            ramo = $('#ramo').val();
            url = '{{route('getRamos')}}';
            data = {ramo: ramo};
            $('#subramos').empty();
            $.ajax({
                url: url,
                headers: {'X-CSRF-TOKEN': token},
                data: data,
                type: 'POST',
                datatype: 'JSON',
                success: function (resp) {
                    $.each(resp.subramos, function (key, value) {
                        $('#subramos').append('<option>'+ value.nombre_subramo +'</option>');
                    });
                }
            });
        });
    });
</script>

Considering "#ramo" as the selected input and using the style / html package, where the token is passed as hidden input.

+7

Laravel 5.2 (, Ajax)

ajax- , :

public function getLev() {
    if (!Request::ajax())
        return false;
    $result = Input::all();
    $lev_id = (int) $result['lev_id'];
    $invoiceid = (int) $result['invoiceid'];
    return SuppliersController::getLev($invoiceid,$lev_id);//you can do any thing with your variables
//function is working in my case, you case take idea from this function
}

public function deleteInvoice() {
    if (Request::ajax()) {
        $data = Input::all();
        return delete_invoice($data['invoice_id'], $data['reason_text']);//you can do any thing with your variables
    }
    return false;
//function is working in my case, you case take idea from this function
}

/, ajax:

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
+2

Laravel 5.2, ajax-. codeigniter $this- > input- > is_ajax_request(). Laravel 5.2 - ?

ajax(), $request->ajax() Request::ajax() ( , ) .

, , csrf. , - "web", csrf, ajax? Laravel ?

, AJAX , CSRF ( - ). : https://laravel.com/docs/5.2/routing#csrf-x-csrf-token

0

At any time when you define an HTML form in your application, you must specify a hidden CSRF token field in the form so that the CSRF middleware can validate the request.

To create a hidden _token input field containing a CSRF token, you can use the csrf_field helper function:

So, in order to use an AJAX request with the POST method, you need to pass the hidden CSRF token field along with ajax data:

<script>

var token="<?php echo csrf_token(); ?>";

$.ajax({

  url:url,
  method:'POST',
  data:{
    '_token':token,
    'id':1    
  }


})

</script>
0
source

All Articles