首页 > 解决方案 > 在 vue.js 中将 url 作为参数传递

问题描述

我将参数传递给也是一个 url 的 url,参数是:profile/foldername/imgname.jpg当我profile/foldername/imgname.jpg作为参数传递时,我只进入imgname.jpg我的 laravel 控制器,但我想将整个 url 作为参数传递。

我正在使用以下代码将图像路径传递给 url:

removeImage: function(index,img_path) {
      var img_path = encodeURIComponent(img_path);
      axios.get(`/api/backend/delete-gallery/${img_path}`, { headers: this.header() }).then((response) => {

在网络选项卡中获取此网址:

http://localhost:8000/api/backend/delete-gallery/post%2FUTS7C210103124422%2F1256160041.png

路线:

Route::get('delete-gallery/{path}', 'API\Controller@deleteGallery');

在控制器中获取它:

dd($request->path);

非常感谢任何帮助。

标签: javascriptvue.jsvuejs2

解决方案


你有很多方法来处理这个问题,我将在下面提到其中的两个:

  • 编码 URI
HTTP://img.com/img.com --> http%3A%2F%2Fimg.com%2Fimg.jpg

并将其发送到后端服务,然后您需要使用urldecode()函数对 PHP 中的字符串进行解码。

let img_path = encodeURIComponent("HTTP://img.com/img.jpg")
  • Base64

您还可以使用btoa()对 URL 进行编码,然后将其发送到后端。您唯一需要做的就是 在 PHP 端使用base64_decode()将 URL 解码为原始 URL。


推荐阅读