Laravel Unexpected Redirects ( 302 )

Laravel unexpected redirects ( 302 )

After several hours of hair pulling, I have found my answer -- and it's silly.

The problem is that the route user.profile has a path user/{uid?} and it matches both user/logout and user/add as paths.

It being before the others, and not having a regex or similar, it handled the route.

I still don't know why a 302 was generated for that page, but found that moving it out of the AuthController and into the UserController (where it should be from the start) fixed the behavior.

Thus, my (amended and working) routes now look like so:

Route::group(['middleware' => 'web'], function () {
// Authentication Routes...
Route::get( 'user/login', ['as' => 'user.login', 'uses' => 'Auth\AuthController@showLoginForm']);
Route::post('user/login', ['as' => 'user.doLogin', 'uses' => 'Auth\AuthController@login' ]);

Route::group(['middleware' => 'auth'], function() {
// Authenticated user routes
Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
Route::get( '/home', ['as'=>'home', 'uses'=> 'HomeController@home']);
Route::get( 'user/logout', ['as' => 'user.logout', 'uses' => 'Auth\AuthController@logout' ]);

// *** Added /profile/ here to prevent matching with other routes ****
Route::get( 'user/profile/{uid?}', ['as' => 'user.profile', 'uses' => 'UserController@profile' ]);
Route::get( '/user/add', ['as' => 'user.add', 'uses' => 'UserController@showAddUser']);

[...]
});
});

Redirect 302 on Laravel POST request

it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

Laravel 302 redirection when I do a POST

You have middleware 'auth' for your route. You should investigate a bit on it and you'll understand why doesn't it work.

The point is that 'auth' middleware requires cookies to work correctly, while you have it empty when you're have Ajax request. Why does it give you 302 and not 401? You should look into your authentication handler for any custom logic on that.

What to do? Implement JWT auth or stateless authentication!

Ajax Post Laravel got 302 Not Found

I don't uunderstand what you want correctly but i propose that:

$.ajax({

type:"POST",

url:"{{ route('users.update') }}",

data:{
'_token': '{{ csrf_token() }}',
"_method" : "POST",
'id_number':id_number,
'id_card_scan':id_card_scan,
'selfie_scan':selfie_scan,
'address':address,
'postal_code':postal_code,
'country':country,
'province_id':province_id,
'city_id':city_id,
'tax_id_number':tax_id_number,
'taxpayer_number_scan':taxpayer_number_scan,
'owner_name':owner_name,
'account_number':account_number,
'bank_id':bank_id,
'branch':branch
},
dataType: "json",
success: function (resp) {
console.log('success');
header = resp.getAllResponseHeaders()
//and use informations in header or call resp to access response body

},

});


Related Topics



Leave a reply



Submit