1
votes

I'm working on a Laravel 5.2.10 project, I was trying to fetch some data via ajax but I'm getting an 500 error, and I can't find what I'm missing out.

This is part of my routes.php

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::get('/home', 'HomeController@index');
    Route::get('/videos', 'VideosController@index');
    Route::post('/videos/fetch', array('before' => 'ajax_check'), 'AjaxController@postFetch');
});

On my 'AjaxController.php' I've got this function

public function postFetch()
{
    //Process data and come up with $data
    return view('videos')->with('video_data', $data);
}

And this is the JS ajax call

var request = $.ajax({
    url: "/videos/fetch",
    method: "POST",
    data: { url : url }
});

request.fail(function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
});

MethodNotAllowedHttpException in RouteCollection.php line 219: in RouteCollection.php line 219 at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206 at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158

2
The MethodNotAllowed hints that your post route isn't being picked up. What does the middleware web do exactly? Your postFetch() method doesn't declare the$data variable, is that because you removed the code to simplify it? Also your post route isn't correct. It should be in the following format Route::post('videos/fetch', array( 'before' => 'ajax_check', 'uses' => 'AjaxController@postFetch' ));Jeemusu
$data is generation is omitted yes. middleware web manages the session info. I believe that the problem is at route.php but I'm not sure what is it, something regarding the way that POST data is managed.jonystorm
Do you have route caching enabled? Does php artisan routes show the expected routes?Jeemusu
I found that it's a different error, there seems to be a "TokenMismatchException". Probably I have to add the csrf token to the ajax request, I'll try with thatjonystorm

2 Answers

2
votes

The MethodNotAllowed exception hints that your post route isn't being picked up. The format of your post route looks a little odd to me. It should be in the following format

Route::post('videos/fetch', array( 'before' => 'ajax_check', 'uses' => 'AjaxController@postFetch' ));

0
votes

Have you set permissions on your storage folder ? Please check your internal php server errors for more info with the command:

tail /var/log/php_errors.log