473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4.  
  5. <script type="text/javascript">
  6.  
  7. function objetoAjax(){
  8.     http_request= false;
  9.     if (window.XMLHttpRequest) { // Mozilla, Safari,...
  10.         http_request = new XMLHttpRequest();
  11.         if (http_request.overrideMimeType) {
  12.            http_request.overrideMimeType('text/xml');
  13.         }
  14.      } else if (window.ActiveXObject) { // IE
  15.         try {
  16.            http_request = new ActiveXObject("Msxml2.XMLHTTP");
  17.         } catch (e) {
  18.            try {
  19.               http_request = new ActiveXObject("Microsoft.XMLHTTP");
  20.            } catch (e) {}
  21.         }
  22.     }
  23.     return http_request;
  24. }
  25.  
  26. function devolver_resultado(){
  27.     var llamadaAjax = objetoAjax();
  28.     var codigo = document.getElementById('codigo').value;
  29.     llamadaAjax.open("POST",'Compiler',true);
  30.     llamadaAjax.onreadystatechange = function() {
  31.         if(llamadaAjax.readyState == 4){
  32.             document.getElementById("resultado").innerHTML = llamadaAjax.responseText;
  33.         }
  34.     };
  35.     llamadaAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  36.     llamadaAjax.send('codigo='+codigo);
  37. }
  38.  
  39. </script>
  40.  
  41.  
  42. </head>
  43.  
  44. <body>
  45.  
  46. <form action="Compiler" method="post">
  47.     <textarea rows="18" cols="70" id="codigo" name="codigo"></textarea>
  48.     <input type="submit" value="Compilar" onclick="devolver_resultado(); return false;">
  49. </form>
  50.  
  51. <div id="resultado">
  52. </div>
  53.  
  54. </body>
  55. </html>
When debugging, I added a breakpoint right after var codigo = document.getElementById('codigo').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.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!!
May 1 '14 #1
1 1327
SOLVED.

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

Expand|Select|Wrap|Line Numbers
  1. llamadaAjax.send('codigo='+encodeURIComponent(codigo));
May 1 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Luke Matuszewski | last post by:
Hi ! I have faced the problem of checking that the user has entered the unicode letter (not only ASCII set of letters...). It seems that ECMAScript 3rd regular expressions do not include posix...
3
by: mudgilgaurav | last post by:
I am very keen to learn AJAX and want to use it in my website Can anyone give me a bunch of code abt ajax and as well as tell me the flow of code
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
3
by: bss | last post by:
Dear All, I have developing a French website using PHP & Ajax. In that I tried to display some French texts from mysql database using Ajax. Form local I got the text from db with Correct...
2
by: awi | last post by:
Hi Everyone, I'm facing a unique problem when testing with FireFox (at least, I think its unique). Here is the situation: I have a page setup that looks like this: <form id="abcForm"> <div...
6
by: dmorand | last post by:
I'm having a little trouble with my ajax. I can see my results in IE, but not firefox. I'm assuming I'm missing some syntax somewhere. alert("Test " + results + testing); returns the values in...
1
by: biggie3384 | last post by:
Is it possible to submit a form using ajax? I have a form with sever inputs and when submited, it writes the input values in a jsp file. I would like to submit the form using ajax in the...
5
by: mukeshrasm | last post by:
Hi I am displaying a page (result.php) using ajax and in this page I have few links when clicked opens a div popup. in which a form, a text field and a submit button is there. when I click submit...
1
by: mfaisalwarraich | last post by:
I am trying to get values posted by a form using ajax but i dont know how to get them. im opening a form which is also based on the ajax. here is the code. function showPage(page) { var...
4
by: Stoic | last post by:
Hi, I have a form and I have place an ActiveX Control(WebBrowser) that will populate a web page with strolling texts, I have developed in html. In the form 'On Current Event' I have placed a code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.