0

I am trying to send a message using the XmppClient.js of kaazing, but I get an : SyntaxError: JSON.parse: unexpected character. The code in javascript is :

var client = new XmppClient(...);
var txtClient = new XmppRoom(...); 

sendFromEditor : function(char){
                var json = JSON.stringify(char);
                //alert(json);
                txtClient.sendMessage(json);
            };

and I am receiving messages :

txtClient.onmessage = function(msg) {

var data = JSON.parse(msg.body);

alert(data);

The problem is in JSON.parse. The messages I am trying to send are very small (one char). I also tried to change the maximum message size of kaazing gateway just in case but with no lack!

Any ideas?

Thanks in advance.

jpap
  • 11
  • 3
  • And what is the value of `msg.body`, when `JSON.parse()` fails? – Sirko Feb 11 '14 at 20:32
  • Nothing, I get the error value in the console! – jpap Feb 11 '14 at 20:45
  • So you receive an empty message? I highly doubt that. Do a `console.log()` on `msg` **before** `JSON.parse()`. – Sirko Feb 11 '14 at 20:54
  • Sorry I must make a correction : My initial goal was not to send only a char but the var jsonData = JSON.stringify([[command, args], ]); //alert(jsonData);. Before JSON.parse I could see what I had passed with an alert or console.log but when it arrived to JSON.parse I was getting the aforementioned error! – jpap Feb 11 '14 at 21:27
  • So back to the question: what *exactly* is the console output before the `JSON.parse()`? – Sirko Feb 11 '14 at 21:31
  • Alert and console.log shows the msg correctly as I send it and then Error JSON.parse: unexpected character! – jpap Feb 11 '14 at 21:39
  • The error states there is an unexpected character in that message. If you don't want to show that message, I can not help you there. Probably you try to use `JSON.parse()` on an already parsed object, but I can not verify that this way. – Sirko Feb 11 '14 at 21:41
  • Thank you for your time. This is what I get after the JSON.stringify : [["join_session",["jpap"]]] and is sended with the sendMessage(). I will keep trying and let you know. – jpap Feb 11 '14 at 22:44

1 Answers1

-4

I could suggest to surround it with a try catch to check the exact error.. not sure if that helps ;)

Rainer
  • 1