2

I know this has been asked a few times but please bear with me.

I have a google maps object which is rather complex (it contains various nodes, coordinates, etc) and I am trying to pass it as a query string.

I need a play javascript/jQuery solution.

I have tried the .param method which gives a jQuery error. The only thing that works is the "stringify" method which then creates a string that when appearing as a url looks a bit like this: %7B%5C"shape_1%5C"%3A%7B%5C"color%5C"%3A%5C"%237F0000%5C"%2C%5C"data%5C"%3A%7B%5C"b%5C"%3A%5B%7B%5C"Na%5C"%3A51.56727431757122%2C%5C"Oa%5C"%3A-0.10462402858888709%7D%2C....

php translates that as: {\\"shape_1\\":{\\"color\\":\\"#7F0000\\",\\"data\\":{\\"b\\":[{\\"Na\\":51.56727431757122,\\"Oa\\":-0.10462402858888709},...

but having said that I don't want to use PHP, I am just showing you what it does in case it helps you see what stringify did to the object.

After I unescape with Javascript it looks a bit more normal like:

{\"shape_1\":{\"color\":\"#7F0000\",\"data\":{\"b\":[{\"Na\":51.56727431757122,\"Oa\":-0.10462402858888709},..

So as you can see, the unescaped sequence has these slashes everywhere. When I try to evaluate that into a JSON object I get "Illegal token \". The parse method also fails. I just can't find any way to put this string back into the complex JSON object that it was. I have looked online for various suggestions but they fail. I also don't understand why stringify injects all these slashes which simply shouldn't be there. If anyone has an idea how to take that object, put it in a query string and then parse it back I would be very grateful.

Nick


Update: The answer is this:

encodeURIComponent(JSON.stringify(myObject));

And then on the receiving end:

var a = querySt("data");
var b = decodeURIComponent(a);
var c = unescape(b);
var d = JSON.parse(c);

or all in one line

JSON.parse(unescape(decodeURIComponent(querySt("data"))));

Nick

Nick
  • 2,877
  • 2
  • 33
  • 62
  • I would avoid trying to put JSON into query strings if at all possible. With a modest amount of data, you can fairly easily run into limits on length. – Michael Mior Oct 31 '11 at 23:57
  • Can you perhaps think of a way to pass those coordinates from a form to an email and from the email back to another html page? – Nick Nov 01 '11 at 11:37
  • What do you mean by passing coordinates from a form to an email? Do you want to send an email which contains the coordinates? And then have another email sent which creates an HTML page? – Michael Mior Nov 01 '11 at 15:42

2 Answers2

0

Try this to convert query string into json object

var queryStringToJSON = function (url) {
    if (url === '')
       return '';
    var pairs = (url || location.search).slice(1).split('&');
    var result = {};
    for (var idx in pairs) {
    var pair = pairs[idx].split('=');
    if (!!pair[0])
        result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
    }
   return result;
}

You can use jQuery.param method to covert json object back to query string

Premchandra Singh
  • 14,156
  • 4
  • 31
  • 37
0

See http://php.net/manual/de/security.magicquotes.php - you have to turn off magic quotes. They are old, deprecated stuff, they are insecure and break stuff.

Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed.

Howto: http://www.php.net/manual/de/security.magicquotes.disabling.php

thejh
  • 44,854
  • 16
  • 96
  • 107
  • Ah yes, but I need a non PHP solution. Ideally the JSON string should be encoded and decoded using jQuery.. Is there a solution for that? And also why does the "stringify" method of jQuery add magic quotes? I am not using PHP for any part of the process – Nick Oct 31 '11 at 22:08
  • @Nick: Hmm, what? You wrote "but php translates that as". – thejh Oct 31 '11 at 22:26
  • Yes that's what PHP does but I don't use that. Ideally I want to use plain html – Nick Oct 31 '11 at 22:45
  • @Nick: So, what happens when you do just JSON.parse(decodeURIComponent(encodeURIComponent(JSON.stringify(yourObject))))? – thejh Oct 31 '11 at 22:52
  • Ok that doesn't work. If I do encodeURIComponent(JSON.stringify(myObject)) I get a string like this.. %7B%22shape_1%22%3A%7B%22color%22%3A%22%237F0000%22%2C%22data%22%3A%7B%22b%22%3A%5B%7B%22Na%22%3A51.566634089631%2C%22Oa%22%3A... And if I do JSON.parse(decodeURIComponent(query_string)) I get a javascript error: "unexpected token %" so it seems like "encodeURIComponent" cannot be decoded without leaving trailing % behind...did you try it too? – Nick Oct 31 '11 at 23:41
  • After doing this encodeURIComponent(JSON.stringify(myObject)) I got it work by doing something like this: var a = querySt("data"); var b = decodeURIComponent(a); var c = unescape(b); var d = JSON.parse(c); basically the unescape makes all the difference. Thank you guys for the help – Nick Oct 31 '11 at 23:52