I am using PUSHER into my site for notification. I successfully added the codes and its working. But the problem is when the alert is getting triggered I am receiving messages but its not what I am looking for.
I am receiving this message from Javascript alert();
{"message":"A new appointment arrived"}
alert message from pusher And my code is
var channel = pusher.subscribe('my-channel'); channel.bind('my-event', function(data) { document.getElementById('audio').play(); alert(JSON.stringify(data)); $.ajax({url: "notification", success: function(result){ $("#notification").html(result); }}) });
And this is where I am getting this.
$data['message'] = 'A new appointment arrived'; $pusher->trigger('my-channel', 'my-event', $data);
I am getting the message from
JSON.stringify(data)
My question is, is there a way I can remove everything except A new appointment arrived
from the alert message?
Thanks in advance.
I am a beginner and I have very little knowledge about Javascript.
Advertisement
Answer
If I’m understanding your question correctly, what you’re asking is how you change the output that gets shown in the alert from
{"message":"A new appointment arrived"}
to just A new appointment arrived
. Is that right?
What JSON.stringify()
does is convert a JavaScript object into a string of text in the JSON format. Since you’re not actually interested in the object, just the message it contains, there really isn’t any need to use JSON.stringify
here. Assuming that the data you’re receiving always has the format
{ message: "Some type of message" }
you can just write alert(data.message)
(or alert(data["message"])
if you don’t like JavaScript dot notation).