How to correctly encode text sent over a form using ajax?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patr1c1a
    New Member
    • May 2014
    • 2

    How to correctly encode text sent over a form using ajax?

    I have a java servlet (called "Compiler") that gets some java code sent from an html form, compiles the code and returns the results (if there's an error, it indicates in what line and what kind of error). I use ajax to display those results in the same html page that contains the form.
    The thing is, if I just use the form without any ajax, everything works as expected. But when I add the ajax scripts I get some characters removed or the whole text after them completely stripped out (for example: +, =, % and so on).

    This is my html:
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    
    function objetoAjax(){
    	http_request= false;
    	if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	    http_request = new XMLHttpRequest();
    	    if (http_request.overrideMimeType) {
    	       http_request.overrideMimeType('text/xml');
    	    }
    	 } else if (window.ActiveXObject) { // IE
    	    try {
    	       http_request = new ActiveXObject("Msxml2.XMLHTTP");
    	    } catch (e) {
    	       try {
    	          http_request = new ActiveXObject("Microsoft.XMLHTTP");
    	       } catch (e) {}
    	    }
    	}
    	return http_request;
    }
    
    function devolver_resultado(){
    	var llamadaAjax = objetoAjax();
    	var codigo = document.getElementById('codigo').value;
    	llamadaAjax.open("POST",'Compiler',true);
    	llamadaAjax.onreadystatechange = function() {
        	if(llamadaAjax.readyState == 4){
        		document.getElementById("resultado").innerHTML = llamadaAjax.responseText;
            }
        };
        llamadaAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        llamadaAjax.send('codigo='+codigo);
    }
    
    </script>
    
    
    </head>
    
    <body>
    
    <form action="Compiler" method="post">
        <textarea rows="18" cols="70" id="codigo" name="codigo"></textarea>
        <input type="submit" value="Compilar" onclick="devolver_resultado(); return false;">
    </form>
    
    <div id="resultado">
    </div>
    
    </body>
    </html>
    When debugging, I added a breakpoint right after var codigo = document.getEle mentById('codig o').value;
    and found out that the variable "codigo" is getting the correct string assigned:


    But then I check the request sent by the browser to my servlet and it's completely different:



    I'm not exactly sure, but I suspect the problem can be in this line: llamadaAjax.set RequestHeader(" Content-Type","applicat ion/x-www-form-urlencoded")
    However, I tried replacing "applicatio n/x-www-form-urlencoded" with "multipart/form-data" and I get a 500 server error.

    How can I fix this?

    Thanks!!
  • patr1c1a
    New Member
    • May 2014
    • 2

    #2
    SOLVED.

    All I needed was to encode the text before sending it:

    Code:
    llamadaAjax.send('codigo='+encodeURIComponent(codigo));

    Comment

    Working...