troubleshooting Question

Problem with character encoding in AJAX

Avatar of elvin226
elvin226 asked on
JavaScriptASPXML
8 Comments1 Solution930 ViewsLast Modified:
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.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros