Encoding problem with new Ajax.Request

111 views
Skip to first unread message

Uzm

unread,
Oct 20, 2008, 2:12:08 PM10/20/08
to Prototype & script.aculo.us
I've a small problem with new Ajax.Request. Actually, there isn't
really a problem with the function itself, but with encoding.
Here is my code:

function getData() {
new Ajax.Request('chat.php', { method: 'post', encoding:
'windows-1251', parameters: "userId=" + userId + "&msgtime=" +
msgtime,
onComplete: function(getDataRequest) {
if (getDataRequest.responseText.substr(0,1) == "|") {
clearTimeout(yo);
alert(getDataRequest.responseText.substr(1,getDataRequest.responseText.length
- 1));
exit();
}
else {
var lines = getDataRequest.responseText.split('\n');
lines.length = lines.length - 1;
if (lines.length > 0) {
onlineUserList = lines[0].split("|");
onlineUserList.length = onlineUserList.length - 1;
}
if (lines.length > 1) {
for (i = 1; i < lines.length; i++) {
var msg = lines[i].split("|");
try {
parent.talk.printmsg(msg[0],msg[1],msg[2],msg[3]);
} catch (e) {
}
msgtime = msg[4];
}
}
seichas = document.getElementById("chatlan");
mysp = document.createElement("span");
hrprfr = document.createTextNode("ЮЗЕРОВ ОНЛАЙН: " +
onlineUserList.length);
mysp.appendChild(hrprfr);
seichas.replaceChild(hrprfr, seichas.firstChild);
parent.menu.printmenu();
}
yo = setTimeout('getData()', chatTimeout);
},
onFailure: function(transport) {
alert('XHR failed!\nReason:' + transport.responseText)
} } );
}

The problem is in:
"&msgtime=" + msgtime
Msgtime is selected from the database before it is sent. In the table,
Msgtime type is "TIME", which shows current time in H:i:s (hours,
minutes, seconds).

I've debugged the request with "SoftX HTTP Debugger", and here's the
request:

POST /chat.php HTTP/1.1
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: sv
x-prototype-version: 1.6.0.3
Referer: http://www.talk.ee/
x-requested-with: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=windows-1251
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.
1; .NET CLR 1.1.4322)
Host: www.talk.ee
Content-Length: 29
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: N=Uzm; PHPSESSID=8a8ebf98310cf9ed6f3ea0e8091a1904

userId=1&msgtime=21%3A09%3A48

As you can see, msgtime is "21%3A09%3A48", althought it should be
"21:09:48".
I've triend with encodeURIcomponent(), but it doesn't seem to work.

Could you guys help me out with this one?
Thanks.

Saiful Amri

unread,
Oct 21, 2008, 2:23:51 AM10/21/08
to prototype-s...@googlegroups.com
Dear...
try using
....&msgtime="+encodeURIComponent(msgtime)

I think it does work, thanks

Saiful Amri

Uzm

unread,
Oct 21, 2008, 9:22:52 AM10/21/08
to Prototype & script.aculo.us
No, i doesn't work, msgtime is still "msgtime=16%3A22%3A08".

On Oct 21, 8:23 am, "Saiful Amri" <saiful.a...@gmail.com> wrote:
> Dear...
> try using
> ....&msgtime="+encodeURIComponent(msgtime)
>
> I think it does work, thanks
>
> Saiful Amri
>
>
>
> On Tue, Oct 21, 2008 at 1:12 AM, Uzm <uzm.i...@gmail.com> wrote:
>
> > I've a small problem with new Ajax.Request. Actually, there isn't
> > really a problem with the function itself, but with encoding.
> > Here is my code:
>
> > function getData() {
> > new Ajax.Request('chat.php', { method: 'post', encoding:
> > 'windows-1251', parameters: "userId=" + userId + "&msgtime=" +
> > msgtime,
> > onComplete: function(getDataRequest) {
> > if (getDataRequest.responseText.substr(0,1) == "|") {
> > clearTimeout(yo);
>
> > alert(getDataRequest.responseText.substr(1,getDataRequest.responseText.leng-th
> > x-requested-with <http://www.talk.ee/x-requested-with>: XMLHttpRequest

Baglan

unread,
Oct 21, 2008, 9:24:36 AM10/21/08
to Prototype & script.aculo.us
If that's the only problem, why not deal with it on the server. I see
that you're using PHP. That would be as easy as:

$msgtime = str_replace('%3A',':',$_REQUEST['msgtime']);

Considering that message is received by the server-side script almost
instantly, you could forgo sending the time at all and instead
determine the time on the server:

$msgtime = date('H:i:s');

Additionally, setting { encoding: 'windows-1251' } doesn't really do
much as request is sent in UTF-8 and encoding of the reponse is
determited by what encoding is set by the server. In order to be able
to receive Windows-1251 you should set encoding for the message in
your script:

header('Content-type: text/html; charset=windows-1251');

Tho it's not really my business, I'd suggest you to think about
parameters you're sending. You're passing userId as a parameter to the
server which means that anyone could fake such a request and pose as a
different user. I'd use sessions instead.

Good luck with the service
- Baglan

Uzm

unread,
Oct 21, 2008, 12:23:43 PM10/21/08
to Prototype & script.aculo.us
Thanks, Balgan, for a fast and good response.
I've followed your "instructions", but somehow the msgtime is still
shown in the wrong format (as, i said, it's showing %3A instead of
":").
About the userId parameter. Yes, you're right, now i noticed what a
silly thing i did. Thanks!
The main problem still remains... I've tried with rewriting the AJAX-
request with own createRequest() function, and it receives the time as
it supposed to be. So, i think it's a problem on Prototype's side.
Reply all
Reply to author
Forward
0 new messages