Grace
Grace

Reputation: 299

Laravel Ajax Get and Post request using controller

i'm trying to change the $MONTH inside my controller from my view using ajax but i'm confuse on how to do this, should i use POST or GET method? and i want to use input or select html element please correct me if i'm doing this wrongly thanks.

CONTROLLER:

public function days(Request $request){
$days=array();
$id = $request->input('months');
$month = $id;
$year = 2018;
for($d=1; $d<=31; $d++) {
$time=mktime(12, 0, 0, $month, $d, $year);  
if(date('m', $time)==$month && date('w', $time)>0 && date('w', $time)<6) {
$days[]=date('Y-m-d H:i:s', $time);
}}


$.ajaxSetup({
    headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
ajax({
    url:'days',
    type: 'get',
    data: {

       months:1
    },
    success: function( data ){

        console.log(data);
    },
     });

Route::

Route::get('days', 'PivotController@days' ); 

Upvotes: 1

Views: 316

Answers (1)

manjinder
manjinder

Reputation: 95

enter code hereYou can use ajax like that it will definetly work.

jQuery.ajax({
    url: '<?php echo url("/ajax-data-type");?>', // Url to which the request is send
    type: "POST", // Type of request to be send, called as method
    data:{"month":1,"_token": "{{ csrf_token() }}"},
     success: function (response)   // A function to be called if request succeeds
        {
          jQuery(".loader-wrapper").addClass('hide');
          $('#ajax_data').html(response);
           if(response.status == false){
              SITE.initNotifications('alert-danger', '', response.result);
              }
        }
    });

Upvotes: 1

Related Questions