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
The Behaviour of the or Operator in PHP
How to Access Outer Local Variable in PHP
Laravel 5 Auth Post Submit - Tokenmismatchexception in Verifycsrftoken.PHP Line 46
Deny Direct Download of File Using PHP
Png Transparency Resize with Simpleimage.PHP Class
Why Use Output Buffering in PHP
How to Return Only Named Groups with Preg_Match or Preg_Match_All
How to Pass an Array into a PHP Soapclient Call
Wordpress Paginate_Links - How to Use It
What Is the Js Equivalent to the PHP Function Number_Format
PHP Script to Log the Raw Data of Post
Php: Require Path Does Not Work for Cron Job
Iconv_Strlen Function Causing Execution Timeout, Running on Mamp
Object Copy Versus Clone in PHP
PHP - Regex - How to Extract a Number with Decimal (Dot and Comma) from a String (E.G. 1,120.01)
Mongodb: Benefit of Using Objectid VS a String Containing an Id