Link to home
Create AccountLog in
Avatar of elvin226
elvin226

asked on

Problem with character encoding in AJAX

With AJAX, I'm having problem when submitting text that includes ascii characters (that are not readily available on the keyboard, e.g., ©, ñ, ®, ™). I found that these characters are sent/submitted in UTF-8, which is the default charset when using the send() function of Microsoft.XMLHTTP ActiveXObject, and appear differently when inserting into the database. The ñ comes as ñ, the © comes as ©, and so on. There is always this  or à character that is stored together with original character. If I have a char field in the database with a length of 1, it is apparent that this kind of encoding is a problem.

Here is a sample code that I use:

var oXMLHTTP;

function submitForm()
{
    var sParams;

    oXMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');

    with (oXMLHTTP)
    {    
        onreadystatechange = showSubmitResult;
        open('POST', 'saveData.asp?sid='+Math.random, true);
        setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        sParams = 'txtText=' + document.forms[0].txtText.value
        send(sParams)
    }
}

A documentation I found on MSDN (http://msdn2.microsoft.com/en-us/library/ms755436.aspx, you might find it funny that IE7.0 detects this Microsoft website as a phising website) says that on the setRequestHeader method, I should include a charset attribute to override the default utf-8 encoding so I tried this:

        setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=iso8859-1');

Or this:

        setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        setRequestHeader('charset, 'iso8859-1');

Both didn't work.

I could just include the data to be submitted in the sUrl parameter of the open() method and retain the original encoding (iso8859-1) but there's another problem: the maximum URL length is 2083 on IE (http://support.microsoft.com/kb/208427/en-us)

I just want to know how I can pass the data thru the send() method while retaining the iso8859-1 encoding.
ASKER CERTIFIED SOLUTION
Avatar of B_Dorsey
B_Dorsey

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of B_Dorsey
B_Dorsey

also in your ASP page you might wanna put at the top

<%@codepage=65001%>
Avatar of elvin226

ASKER

At least one character is not being handled by the escape function and that is the plus (+) sign. I have to explicitly replace it with a hex value of 2B. Are there any other characters that I should check too?
are you POST 'ng or GET 'ng your AJAX request?
nevermid that  i read your code again...
you can try encodeURI instead
I tried the encodeURI but it converts the ñ to ñ
Anyway, your first suggestion combined with replacing the + with hex 2B works on all the characters in the iso8859-1 encoding so I think my problem is now solved. I pasted the contents of this page: http://www.bbsinc.com/iso8859.html to my "input element" (a div with contenteditable=true) and it worked.