繁体   English   中英

307 http响应与laravel的ajax调用

[英]307 http response on a ajax call with laravel

当我尝试通过Ajax调用发布请求时,我在laravel 5.4项目中遇到问题。 在我的本地主机中,它可以正常工作,但是当我将项目移至生产服务器时,它会给我307的响应。

这是我的代码:

.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\HomeController@index');

}

web.php中的路由定义

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

jQuery Ajax代码:

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

错误: 在此处输入图片说明

解决方案实际上是您的问题。

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

您只需要像这样更新网址即可:

'/admin/property/toggleVisiblity'无斜杠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM