musicvicious
musicvicious

Reputation: 1093

Laravel AJAX request, Method not allowed

I have a problem when trying to upload an image via bluimp's jQueryFileUpload.

In my routes i have this: Route::post('image/upload/{folder}', 'ImageController@upload');

my file input that is outside the <form> tags because it is independent to the form:

<input id="imageupload" type="file" name="image" multiple="" data-url="{{ url('admin/image/upload/members') }}" >

my jQuery function points to the data-url attribute value.:

  $('#imageupload').fileupload({
        dataType: 'json',
        maxFileSize: 5000000,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        done: function (e, data) {
            Members.handle_image(data);
        }
   });

The weird thing is that when i call this method from example.app/admin/members/create it works, but when i'm trying to access it from example.app/admin/members/1/edit i get a 405, Method not allowed.

In both cases, the Method is POST.

My routes for create and edit URIs:

Route::get('members/create', [
    'uses' => 'MembersController@create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
    'uses' => 'MembersController@edit', 'as' => 'admin/members/edit'
]);

I'm sure is something really stupid that i can't see.

PS. I have a Project resource, where i also upload images, using the same route and function. It works on both cases (create and edit).

Anybody had this problem ?

Thank you!

Upvotes: 2

Views: 5869

Answers (2)

musicvicious
musicvicious

Reputation: 1093

Ok, i managed to solve this, but really i don't understand why it was not working.

In my routes i have this, where the ajax url points as POST:

Route::post('image/upload/{folder}', 'ImageController@upload');

This did not work. I changed it to:

Route::any('image/upload/{folder}', 'ImageController@upload');

And now it works.

It is strange because on my request headers i have POST method, but with post (in routes) i did not work.

Upvotes: 2

Mhowell
Mhowell

Reputation: 139

HTTP 405 indicates that the request method is not supported.

Both of your routes listen for get requests

Route::get('members/create', [
    'uses' => 'MembersController@create', 'as' => 'admin/members/create'
]);
Route::get('members/{member}/edit', [
    'uses' => 'MembersController@edit', 'as' => 'admin/members/edit'
]);

are you sure that you don't want one, or both to be post?

Route::post('members/create', [
    'uses' => 'MembersController@create', 'as' => 'admin/members/create'
]);
Route::post('members/{member}/edit', [
    'uses' => 'MembersController@edit', 'as' => 'admin/members/edit'
]);

Upvotes: 0

Related Questions