19

我想使用 urlfetch 用页面数据填充电子表格,但我尝试使用的 URL 以错误作为无效参数返回。我认为问题在于我在 URL 中使用了被误解的字符(例如引号和括号)。

我尝试使用下面的命令对 URL 进行编码,但我假设我对某些字符进行了双重编码,这会导致问题。

var encodedURL = encodeURIComponent(pageURL)
4

2 回答 2

25

尝试使用

baseURL + encodeURIComponent(parameterString)

您将传递给要查询的基本 URL 的参数作为传递给 encodeURIComponent 函数的值包含在其中。这篇文章可能对你有用:

在 JavaScript 中编码 URL?

如果您对整个 URL 进行编码,就像您在上面所做的那样,那么您编码的不仅仅是参数,我认为这将是您的问题所在。

于 2012-08-29T20:17:33.653 回答
2

您个人可能不需要答案,但我为需要的人写信。为了对 GitLab 进行成功的 API 调用(API v4),您只需对“变量”进行编码。见下文:

var url = baseUrl + projectId + "/repository/files/" + encodeURIComponent(pathAndFileName) + "?branch=" + encodeURIComponent(branch) + "&author_email=" + encodeURIComponent(authorEmail) + "&author_name=" + encodeURIComponent(authorName) + "&content=" + encodeURIComponent(content) + "&commit_message=" + encodeURIComponent(commitMessage);

wherepathAndFileName是前面定义的变量。

于 2018-08-01T18:19:07.143 回答