views:

436

answers:

2

I am using following code

$.post("insertPrivateMessage?action=sendchat", 
    { to: GroupUserArray[count], 
      message: message, 
      username: $("#author").val(),
      GROUP: chatboxtitle
    } , 
   function(data){
      message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
});

but when replacing message getting following error

message.replcace is not a function

is my code OK?

A: 

check your web directory where you have deployed. Looks like a typo to me, my assumption would be that the javascript you have there is sitting in a cache somewhere. Try refreshing your test website or Ctrl+F5 refresh it.

Spence
+1  A: 

The message variable does not exist in the function. The object is a list of variables sent to the server, the function after it is the function run when the response comes. The message variable does not exist in that function.

I'm not sure what you are trying to do. If you are trying to replace the text before you send it to the server, then you need to use the following code:

$.post("insertPrivateMessage?action=sendchat", 
    { to: GroupUserArray[count], 
      message: message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;"), 
      username: $("#author").val(),
      GROUP: chatboxtitle
    } , 
   function(data){
});

If you are trying to replace the data returned by the server, then you need to use this code:

$.post("insertPrivateMessage?action=sendchat", 
    { to: GroupUserArray[count], 
      message: message, 
      username: $("#author").val(),
      GROUP: chatboxtitle
    } , 
   function(data){
      message = data.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
});
Marius