使用laravel对ajax调用进行307 http响应

时间:2017-07-02 00:12:55

标签: php jquery ajax laravel .htaccess

当我尝试通过Ajax调用post请求时,我在laravel 5.4的项目中遇到了一个问题。在我的本地主机中它完全有效,但是当我将项目移动到生产服务器时,它给了我一个307响应。

这是我的代码:

.htaccess文件:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="root-content-padder">

  <div class="container">

    <h1><strong>"</strong>Die Freiarbeit ist nur das Ergebnis deiner Basis</h1>

    <div class="content-shadow">

      <div class="text-center st-padding">
        <iframe style="width: 50vw; height: 230px" src="youtube.com...."></iframe>

      </div>
      <div class="text-center st-padding">
        <button class="btn btn-default btn-lg">Text</button>
        <strong>Or</strong>
        <button class="btn btn-default btn-lg">Text</button>
      </div>

    </div>
  </div>

  <div style="height: 25px;"></div>
  <div class="container" style="padding-bottom: 10px;">
    <div class="content-shadow">

      <div class="col-lg-4">
        <h1>LoL</h1>
      </div>
      <div class="col-lg-8">

        <p>Text</p>
        <br/>
        <a class="btn btn-default btn-lg" href="?page=me">Weiter lesen</a>
      </div>
    </div>
  </div>
</div>

进入控制器的功能:

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

web.php中的路由定义

    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');

}

Jquery Ajax代码:

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

错误: enter image description here

1 个答案:

答案 0 :(得分:2)

解决方案实际上在你的问题中。

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

您只需更新您的网址:

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