简体   繁体   English

区分二维码 url 和 api url 的参数

[英]distinguishing qrcode url parameters from those of the api url

I want to generate a qr code with multiple pre-filled values.我想生成一个带有多个预填值的二维码。 But only the first value seems to be encoded into the generated qr code.但似乎只有第一个值被编码到生成的二维码中。 I guess because it's assuming parameters after the first belong to the api url..我猜是因为它假设第一个参数之后的参数属于 api url..

Can anyone help me here?有人能帮我一下吗?

.src = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" 
+ "https://google.com/?" + "field1=" + var1 + "&" + "field2=" + var2

I've looked online without luck - perhaps because I don't know how to describe the problem.我在网上看过没有运气 - 也许是因为我不知道如何描述这个问题。 (note that google.com is jut a placeholder url, and not the one i'm using) (请注意,google.com 只是一个占位符 url,而不是我正在使用的那个)

Since the URL you are trying to pass to data contains / , ?由于您尝试传递给data的 URL 包含/ , ? and & you have to encode it.并且&你必须对其进行编码。 For instance, the browser wouldn't know whether to pass field2 to the main URL or to the data URL. To encode the URL being passed to data :例如,浏览器不知道是将field2传递给主 URL 还是传递给data URL。要对传递给data的 URL 进行编码:

const url = "https://google.com/?" + "field1=" + var1 + "&" + "field2=" + var2
const src = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" + encodeURIComponent(url)

On an unrelated note, consider using template string to make your code more readable:在一个不相关的说明中,考虑使用模板字符串来使您的代码更具可读性:

const url = `https://google.com/?field1=${var1}&field2=${var2}`
const src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(url)}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM