0

So I'm uses this setup to send a JSON object through PostMessage plugin and on the receiving end I'm using $.parseJSON(result);

The problem is stringify is adding a bunch of + symbols to the object where there are white space.

So here is what I'm sending through postMessage before it is stringifyed.

{type="page", pageName="bla > bla bla > / whatever"}

Then on the receiving end after I run $.parseJSON(result); I'm getting.

{"type":"page","pageName":"bla+>+bla+bla+>+/+whatever"}

How do I escape or not have all the + symbols. If PostMessage is turning it into query string encoding, then can I remove the + symbols from the string before I $.parseJSON().

PostMassage receive method

$.receiveMessage(function(e){
        var params = e.data.split("&");
        var result = {};
        for (var i = 0; i < params.length; i++){
            var tmp = params[i].split("=");
            result[tmp[0]] = unescape(tmp[1]);
        }

     var msg = result['message'];

}

So msg is a string before I parseJSON

$.parseJSON(result['message']); 

I tried to use .replace on the message but that didn't work. example of what message string is:

msg = '{"type":"page","pageName":"bla+>+bla+bla+>+/+whatever"}'

var msg = result['message']replace("+", " ");

Thanks

Chapsterj
  • 6,483
  • 20
  • 70
  • 124
  • 2
    That is *not* caused by JSON. That is caused by [query string encoding](http://en.wikipedia.org/wiki/Query_string#URL_encoding). – user2246674 Jun 05 '13 at 00:08
  • 1
    Convert to Base64 before sending? – Mr. Polywhirl Jun 05 '13 at 00:10
  • To solve this problem: find out *why it is* being encoded/used as a query string and/or *why it is not* being correctly decoded/used as a query string. Maybe there is a (not-quite-working) hack in the PostMessage plugin that uses query strings to communicate between windows? – user2246674 Jun 05 '13 at 00:12
  • The reason for that is postMessage plugin. There isn't much I can do about that. What I need to do is remove it after it is received by postMessage – Chapsterj Jun 05 '13 at 00:17
  • Mr. Polywhirl I thought Base64 could only be used on Strings. I'm sending an object though PostMessage – Chapsterj Jun 05 '13 at 00:34
  • @Chapsterj You don't need to remove what isn't [incorrectly] added. Stop it at the source. – user2246674 Jun 05 '13 at 06:26
  • Mr. Polywhirl Base64 encoding was the way to go. Thanks. – Chapsterj Jun 05 '13 at 14:05
  • @Chapsterj You are hiding the problem *without* fixing it or understanding the source. – user2246674 Jun 05 '13 at 18:04
  • Ok sorry maybe you can explain the problem so I can fix and understand. Thanks – Chapsterj Jun 06 '13 at 13:38

0 Answers0