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:
Expand|Select|Wrap|Line Numbers
- <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>
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.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
However, I tried replacing "application/x-www-form-urlencoded" with "multipart/form-data" and I get a 500 server error.
How can I fix this?
Thanks!!