在单个查询字符串变量中拟合 json 数据

Fitting json data in a single query string variable

我正在编写一个 Web 应用程序以通过 QR 快速交换联系信息。 我使用的 QR api 格式如下:

`http://api.qrserver.com/v1/create-qr-code/?data=MyData&size=400x400`

我将 json 数据格式化为字符串,输出示例:

`http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&Email=testemail@mail.be&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=`

JSON数据:

{"Name":"John Diggle",
"Title":"IT Consultant",
"Organisation":"testcomp",
"Telwork":"0498818587",
"Telhome":"",
"Gsm":"0498818587",
"Email":"testemail@mail.be",
"Website":"www.testwebsite.be",
"Birthdate":"24/04/97",
"Addresswork":"",
"Addresshome":""}

问题是,当您将此 url 放入 QR 生成器时,它只能识别 Name 参数。我明白为什么会这样。

问题是有没有办法使用 javascript 将所有这些数据转换成字符串并在接收端将其转换回?

或者有人知道这个问题的另一个潜在解决方法吗?

您需要 URL 编码 带有您放入 URL:

中的特殊字符的数据
var url = 'http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&Email=testemail@mail.be&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=';

var query = 'http://.../?data=' + encodeURIComponent(url) + '&size=400x400';

这样您就可以在查询字符串中表示像 & 这样的字符。