Laravel 5和怪异的bug:背面花括号 [英] Laravel 5 and weird bug: curly braces on back

查看:75
本文介绍了Laravel 5和怪异的bug:背面花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在Laravel网站上回顾历史时,就会看到这样的答复:

Whenever I go back in history on my Laravel website, the response I see is this:

{}

当我回到之前的位置时,它也显示了那些大括号.

When I go forward to where I was before that, it shows those braces as well.

如果我在具有禁用缓存"选项的Chrome中启动开发人员工具,则不会发生此问题.返回的Content-Type实际上是application/json.在Firefox中没有这种问题.

The problem doesn't occur if I launch Developer Tools in Chrome with Disable Cache option. The Content-Type of what's returned is indeed application/json. In Firefox there's no such problem.

发生这种情况是因为我的中间件之一.我编写了AjaxJson中间件,将所有Ajax请求转换为JSON响应.奇怪的是,当我回顾历史时,Google Chrome发出了Ajax的请求.它包含以下标头:

It happens because one of my Middlewares. I wrote AjaxJson middleware to translate all Ajax requests to JSON response. Weirdly, when I go back in history, Google Chrome makes this request Ajax. It contains this header:

X-Requested-With:XMLHttpRequest

X-Requested-With: XMLHttpRequest

因此$request->ajax()返回true.

这是我的中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;

class AjaxJson
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (!$request->ajax()) {
            return $response;
        }

        if (!$response instanceof Response) {
            return $response;
        }

        return response()->json($response->getOriginalContent(), $response->status());
    }
}

我在做什么错了?

我发现有关Cache-Control响应头的no-store值.单击后退按钮时,它可防止Chrome使用缓存.我创建了一个中间件来设置Cache-Control这样:

I found out about no-store value for Cache-Control response header. It prevents Chrome from using cache when clicking back button. I created a middleware to set Cache-Control like this:

缓存控制:私有,最大年龄= 0,无缓存,无存储

Cache-Control: private, max-age=0, no-cache, no-store

如果您知道解决此问题的更好方法,请告诉我.

Please let me know guys, if you know better way of solving this problem.

推荐答案

尝试在验证后处理请求:

Try to process the request after validations:

public function handle($request, Closure $next)
{
    if ($request->ajax()) {
        return response()->json($response->getOriginalContent(), $response->status());
    }

    return $next($request);        
}

这篇关于Laravel 5和怪异的bug:背面花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆