我使用此代码发送带有json的url
return response()->json([
'code' => '200',
'message' => 'success',
"resource" => url()."/packages/calibre/images/products/".$artwork->image
]);
但是当我尝试测试响应时,会显示链接
http:\/\/localhost:8000\/packages\/calibre\/ etc..
如何解决?它有双斜杠和反斜杠(/)
答案 0 :(得分:5)
这很好用,
$artwork->imag = 'img.jpg';
return response()->json([
'code' => '200',
'message' => 'success',
"resource" => url("/packages/calibre/images/products/{$artwork->imag}")
]);
邮递员的回应:
{
"code": "200",
"message": "success",
"resource": "http://laravel.dev/packages/calibre/images/products/img.jpg"
}
Web浏览器中的响应:
{"code":"200","message":"success","resource":"http:\/\/laravel.dev\/packages\/calibre\/images\/products"}
如果您希望从编码的URL中删除斜杠,请执行以下操作:
return response(json_encode([
'code' => '200',
'message' => 'success',
"resource" => url('/packages/calibre/images/products/')
], JSON_UNESCAPED_SLASHES))->header('Content-Type', "application/json");
网络浏览器中的响应
{"code":"200","message":"success","resource":"http://verimarked.dev/packages/calibre/images/products"}