Ayyoub Dahhane Ayyoub Dahhane - 1 year ago 65
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

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}]

function into the controller :

public function switchVisiblity() {
$id = request()->get('id');
$property = Property::findOrFail($id);
$property->isVisible = !$property->isVisible;
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 :

button = $(this);
id = button.data('id');
type: 'post',
url : baseUrl + '/admin/property/toggleVisiblity/',
data : {id: id, _token: _token},

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download