Prototype AJAX picking up Wrong response from PHP
I have a really confusing problem. I have a PHP script that polls another PHP script through Prototype. Now when I poll this page once everything is fine and it all reponds fine but when I poll a second time it returns an odd reponse.
What I have is a piece of text:
blah £ £
Now when this goes through Prototype first time around it encodes the string correctly using the escape function. Full code is displayed here (escape_function variable is id_opt['escape_function']):
escape_function: escape, // or encodeURIComponent
var new_text = id_opt['escape_function']($F(id + '_edit'));
var params = 'id=' + id + '&content=' + new_text;
if(id_opt['type'] == 'select') {
params += '&option_name=' + $(id + '_option_' + new_text).innerHTML;
}
var ajax_req = new Ajax.Request(
id_opt['save_url'],
{
method: 'post',
postBody: params,
开发者_运维问答 onSuccess: function(t) { alert(t.responseText); EditInPlace._saveComplete(id, t); },
onFailure: function(t) { EditInPlace._saveFailed(id, t); }
}
);
The polled php page saves the information (the piece of text) to db and reponds with the text. Prototype then sets the contents of a div to the returned text.
When I go to submit it again everything is fine until the alert(t.responseText); when prototype shows some unkown characters in place of the £. The strangest thing is the information in the database field is still blah £ £ (which is correct) but prototype responds with blah (?) (?) (the (?) symbols are the unkown symbol deliminators).
I have come to the conclusion that PHP echos the right information but for some reason prototype picks up the wrong information...but there is nothing to do that between the two which has got me really confused. Being a newb at prototype I'm not really that adept at solving this.
Thanks for any help,
I'd use a tool such as FireBug or Charles to evaluate the traffic coming from your PHP script (unless you've already done that). If all is well, check inside of the client code, using alert()
statements to see when the incorrect translation happens to confirm your Prototype suspicions.
I decided to rewrite the script in JQuery and it worked just fine. I came close to getting it to work in Prototype using encoding types but it would add a accent before each pound sign which was not good enough.
So now I rewrote it everything works perfect without a clitch.
精彩评论