473,396 Members | 1,827 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.

Ajax Posting problem

42
Hey everyone.

I have a ready made project to fix where Ajax POST is used to submit information through form by a click. For some reason the post fails about every fourth time and does not save the information. Post goes through function (code below) and error (no description about error) in Firebug points to the last line of the code. The strange thing is that error occurs every time, but sometimes the post gets through. As being pretty new with Javascript Ajax I don't know how to solve this. Help would be highly appreciated and more information will be provided when required. Thanks in advance!

Expand|Select|Wrap|Line Numbers
  1. XHR.prototype._load = function(url,object) {
  2.     var _this = this;
  3.     if (window.XMLHttpRequest) {
  4.         this.http_request = new XMLHttpRequest();
  5.     } else if (window.ActiveXObject) { // IE
  6.         try {
  7.             this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
  8.         } catch (e) {
  9.             try {
  10.                 this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
  11.             } catch (e) {}
  12.         }
  13.     }
  14.  
  15.     this.http_request.onreadystatechange = function(){_this.getRequest()};
  16.     this.http_request.open("POST", url,true);
  17.     this.http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  18.     if (document.getElementById('textarea_url')){ 
  19.     document.getElementById('textarea_url').value += url + "\n\n" + ((object!=false)?get(document.getElementById(object)):'');
  20.     }
  21.     this.http_request.send('xml=<root>' + ((object!=false)?get(document.getElementById(object)):'') +  '</root>');
  22. }
Aug 14 '08 #1
12 1469
acoder
16,027 Expert Mod 8TB
What values are you passing to the _load() function? Can you also show the code for the get() function.

PS. please use code tags when posting code. See How to Ask a Question. Thanks.
Aug 14 '08 #2
zamuel
42
Thank you for the quick answer.

Ok, the load function is passed with URL, where it uses a PHP file to save the information to database. The 'object' is the content of the text field in the form posted.

Here is the get code:

Expand|Select|Wrap|Line Numbers
  1. XHR.prototype.getRequest = function() {
  2.     if (this.http_request!=null) {
  3.         if (this.http_request.readyState==4) {
  4.             if (document.getElementById('textarea_code')) document.getElementById('textarea_code').value = this.http_request.responseText;
  5.             try {
  6.                 var output = this.http_request.responseXML.documentElement;
  7.                 process(output);
  8.             } catch(e) {
  9.                 //alert('niet goed');
  10.             }
  11.         }
  12.     }
  13. }
  14.  
Aug 14 '08 #3
zamuel
42
Hmmmm...tried with code tags, but apparently didn't work.
Aug 14 '08 #4
acoder
16,027 Expert Mod 8TB
I meant the get() function used on lines 19 and 21 in the first post.
Aug 14 '08 #5
zamuel
42
Oh yeah now I got it. Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function get(obj) {
  3.     var getstr = "";
  4.     var i = 0;
  5.  
  6.     if (obj!=false && obj!=null) {
  7.         if (obj.tagName.toUpperCase()!="INPUT" && obj.tagName.toUpperCase()!="TEXTAREA" && obj.tagName.toUpperCase()!="SELECT") {
  8.             if (obj.tagName.toUpperCase()!="FORM") {
  9.                 for (i; i<obj.childNodes.length; i++) {
  10.                     var oo = obj.childNodes[i];
  11.                     if (oo.hasChildNodes() && oo.tagName.toUpperCase()!="TEXTAREA" && oo.tagName.toUpperCase()!="SELECT") {
  12.                         getstr += get(oo);
  13.                     }
  14.                     getstr += get_element(oo);
  15.                 }
  16.             } else {
  17.                 for (i; i<obj.elements.length; i++) {
  18.                     var oo = obj.elements[i];
  19.                     getstr += get_element(oo);
  20.                 }
  21.             }
  22.         } else {
  23.             getstr += get_element(obj);
  24.         }
  25.         return getstr;
  26.     }
  27. }
  28.  
By the way, I am not sure if I am posting this correctly by I am using code tags like ' code=javascript - /code with brackets.
Aug 14 '08 #6
acoder
16,027 Expert Mod 8TB
Don't worry, the code tags are fine. What's the error message displayed in Firebug? Is the object passed an ID of an element on the page?
Aug 14 '08 #7
zamuel
42
I am pretty sure the answer is yes, it's passed as ID.

The page is basically TPL file where text field and submit button are assigned in PHP file. So the template is basically using tag like <td>[[textOpmerking]]</td> as text field identifier.
Aug 15 '08 #8
zamuel
42
The funny part with the firebug error is that it does not describe any error, just displays the red 'X' indicating that something went wrong.
Aug 15 '08 #9
acoder
16,027 Expert Mod 8TB
Is the Console panel in Firebug enabled?
Aug 15 '08 #10
zamuel
42
Yes, it is enabled and showing the red text in case of POST.

In additionally the page is using other form the send emails. This form is using same posting methods and showing same failures in Javascript function's last line (which I posted in the question). As a result sometimes the mail gets send and sometimes it does not.

When I remove the last line in Javascript the error does not occur, but of course the functionality does not work either :) This is a tricky one!
Aug 15 '08 #11
zamuel
42
Ok, this problem is solved by using Jquery functionality. Problem was caused by the fact that code was executing other functions at the same time, simple "async: false" in new solution did the trick. Thanks for the help anyway.
Aug 15 '08 #12
acoder
16,027 Expert Mod 8TB
Glad you got it working. An alternative to a synchronous request is to make the second request when the readyState of the first is complete (4).
Aug 15 '08 #13

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

Similar topics

2
by: chuck | last post by:
Hi, Firstly, I don't know if this is just and isolated thing with the prototype.js library or is a problem in general with ajax calls with a '+' character, so i am posting it here. I am...
4
by: d3vkit | last post by:
I have a form on my page, and some javascript which uses ajax to submit the form, and then opens the new page in a div using ajax so there is no refresh. This works fine. But the problem is this: all...
2
by: K. | last post by:
Hello! I have the following problem. I have form div which is replaced by ajax event. Unofrtunately all the ajax inputs are null after posting the form (method="post") in Firefox, but on...
0
by: BlipBlip | last post by:
Hi All, I was not sure which forum to post the message to since the problem related to ASP/AJAX, but decided to post it here. I have a simple routine which utilizes an Ajax to query database for...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
1
by: Mark B | last post by:
This is my first try at using AJAX. I want the calendars to be enabled if the user checks CheckBox1. It works OK for a normal all page refresh but once I introduced the AJAX code it stopped...
8
by: Tomasz J | last post by:
Hello developers, After migrating my web project application (using the old model) to .Net Framework 3.5 and Ajax Control Toolkit release 20820 the Accordion control no longer works correctly....
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
11
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have run into a situation that if a page/tab that uses the Ajax toolkit (using .net version 3.5) is closed before the Ajax enable controls complete loading, then IE locks up. Does it in both IE7...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.