2017-07-02 112 views
0

当我尝试通过Ajax调用请求时,我遇到了laravel 5.4项目中的一个问题。在我的本地主机上它完美的工作,但是当我将我的项目移到生产服务器时,它给了我一个307响应。307 http响应与laravel的ajax调用

这是我的代码:

.htaccess文件:

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

功能到控制器:

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\[email protected]'); 

} 

路由定义在web.php

Route::post('/property/toggleVisiblity/', '[email protected]'); 

Jquery A JAX代码:

$(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'); 
      } 
     } 
    }); 
}); 

错误: enter image description here

回答

2

的解决方法是在你的问题其实。

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

您只需更新网址象这样:

'/admin/property/toggleVisiblity' - 没有尾随斜线。