javascriptjsonquery-string

Fitting json data in a single query string variable


I am writing a web application to exchange contact information fast via QR. I use a QR API which is formatted like this:

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

I have JSON data formatted in a string, example of output:

`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 data:

{"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":""}

The problem is when you put this URL in the QR generator it only recognizes the Name parameter. I understand why this happens.

The question is is there a way using JavaScript to convert all this data in a string and convert it back on the receiving end?

Or does anyone know another potential fix for this problem?


Solution

  • You need to URL encode data with special characters you put into a 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';
    

    This way you can represent characters like & inside a query string.