Ayyoub Dahhane Ayyoub Dahhane - 3 months ago 16
Ajax Question

307 http response on a ajax call with laravel

I face an issue in my project with laravel 5.4 when I try to call a post request via Ajax. In my local host it perfectly works, but when I move my project to production server, it gives me an 307 response.

This is my code :

.htaccess File :

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=307]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>


function into the controller :

public function switchVisiblity() {
$id = request()->get('id');
$property = Property::findOrFail($id);
$property->isVisible = !$property->isVisible;
$property->save();
if (request()->ajax()) {
return $property->isVisible+" ";
}
return redirect()->action('\App\Http\Controllers\Admin\HomeController@index');

}


Route definition in the web.php

Route::post('/property/toggleVisiblity/', 'PropertyController@switchVisiblity');


Jquery Ajax code :

$(document).ready(function(){
$('.js-property-switchVisibility').click(function(){
button = $(this);
id = button.data('id');
$.ajax({
type: 'post',
url : baseUrl + '/admin/property/toggleVisiblity/',
data : {id: id, _token: _token},
success:function(result){
if(result=="1"){
button.text("Hide");
button.removeClass('btn-success').addClass('btn-danger');
}else{
button.text("Show")
button.removeClass('btn-danger').addClass('btn-success');
}
}
});
});


Error :
enter image description here

Answer Source

The solution is in your question actually.

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=307]

You simply have to update your urls like so:

'/admin/property/toggleVisiblity' -- No trailing slash.