Laravel“格式错误的UTF-8字符,可能编码有误",该如何解决? [英] Laravel "Malformed UTF-8 characters, possibly incorrectly encoded", how to fix?
本文介绍了Laravel“格式错误的UTF-8字符,可能编码有误",该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将通过用户模型中的以下方法获得的json添加的属性返回,但我不断获得
I try to return as json added attribute which I get with the following method in my User model but I keep getting
"message": "Malformed UTF-8 characters, possibly incorrectly encoded",
"exception": "InvalidArgumentException",
"file": "/var/www/timetool/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php",
代码
/**
* @return string
*/
public function getAvatarImageAttribute($value)
{
if($this->hasMedia('avatar')) {
$image = $this->getMedia('avatar');
$img = \Intervention\Image\ImageManagerStatic::make($image[0]->getPath())->encode('data-url');
}
elseif (isset($this->blob->dokument)) {
$img = 'data:image/jpeg;base64,'. base64_encode($this->blob->document);
} else {
$img = '';
}
return $img;
}
在控制器中,我有
return \Response::json($users, 200, array('Content-Type' => 'application/json;charset=utf8'), JSON_UNESCAPED_UNICODE);
推荐答案
我认为这与仅需要UTF8字符的JSON有关,并且您的Blob可能包含无效的字符.尝试utf8_encode($ img). http://at2.php.net/manual/en/function. utf8-encode.php 在您的控制器中只需返回即可. Laravel将为您建立一个适当的json响应.
I'm thinking it's related to JSON needing only UTF8 chars and your blob may have invalid chars. Try utf8_encode($img). http://at2.php.net/manual/en/function.utf8-encode.php In your controller just return. Laravel will build a proper json response for you.
这篇关于Laravel“格式错误的UTF-8字符,可能编码有误",该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文