【问题标题】:force encoding to work on request made for laravel api by web browser强制编码处理 web 浏览器对 laravel api 的请求
【发布时间】:2020-07-08 04:47:30
【问题描述】:

我正在维护一个API,其中有一些由front-end app 制作的requests。当我从postman 调用它时,一切正常,包括它的encodingresponse 是:

{ "message": "Esse nome de usuário está disponível" }

但是,当我从browser 发送时,我会收到:

{"message":"Esse nome de usu\u00e1rio est\u00e1 dispon\u00edvel"}

我已经制作了 this 教程,它创建了一个 middleware 强制 encoding,在我的项目中,我将其设置为这样,与它所建议的代码不同:

$request->headers->set('Accept', 'application/json; charset=UTF-8');
$request->headers->set('Charset', 'utf-8');

postman中,看到requestresponse中没有设置这些headers

另外,我已经尝试在我的controller 回复中设置:

return response()->json([
                'message' => __('auth.nickname_available'), 
            ], 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8']);

我关注了this 提示。但两者都不起作用。

我怎样才能使这项工作顺利进行?

【问题讨论】:

    标签: laravel rest api laravel-5 encoding


    【解决方案1】:

    您是否正确尝试了this 解决方案?以下对我有用:

    return response()->json(
        [
            'Message' => 'Esse nome de usuário está disponível',
        ],
        200,
        ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
        JSON_UNESCAPED_UNICODE
    );
    

    在您的问题回复中,您忘记添加JSON_UNESCAPED_UNICODE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2014-04-05
      • 2017-12-08
      • 2016-07-04
      相关资源
      最近更新 更多