mercredi 23 juin 2021

AJAX data not reaching Laravel Controller method

I am trying to make updates to an app that is using Laravel 5.6. I have the following Laravel set up:

Route

Route::post('getFeedData', 'SurveyController@getFeedData')->name('getFeedData');

Controller

use App\Http\Controllers\Controller;
use App\Classes\FeedRequest;        
class SurveyController extends Controller
{
    public function getFeedData(FeedRequest $request){
        $obj = new FeedRequest();
        
        DebugBar::info($request);
        return response($this->surveyRepository->getNewFeed($obj));
    }
}

My FeedRequest Class

namespace App\Classes;

class FeedRequest {
    public $segments;
    public $locations;
    public $departments;
    public $improvementCats;
    public $search;
    public $waste;
    public $count;
    public $offset;

    public function __construct() {
        $this->segments = array();
        $this->locations = array();
        $this->departments = array();
        $this->improvementCats = 0;
        $this->search = '';
        $this->waste = array();
        $this->count = 20;
        $this->offset = 0;
    }
}

And Lastly My JS

$.ajax({
            url: "/getFeedData",
            type: 'POST',
            contentType: "application/json",
            headers: {
             'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data: JSON.stringify(request),
        })
        .done((response, textStatus, xhr) => {
            ...
        })
        .fail(() => {
            ...
        }) 

My Controller method is executing but I always have the default version of my FeedRequest object. When I change the controller to accept the default Laravel Request object all my passed parameters are in the "content" attribute. What am I missing to properly hydrate the FeedRequest parameter?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire