Ajax Posting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zamuel
    New Member
    • Sep 2006
    • 42

    Ajax Posting problem

    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!

    [CODE=javascript]XHR.prototype._ load = function(url,ob ject) {
    var _this = this;
    if (window.XMLHttp Request) {
    this.http_reque st = new XMLHttpRequest( );
    } else if (window.ActiveX Object) { // IE
    try {
    this.http_reque st = new ActiveXObject(" Msxml2.XMLHTTP" );
    } catch (e) {
    try {
    this.http_reque st = new ActiveXObject(" Microsoft.XMLHT TP");
    } catch (e) {}
    }
    }

    this.http_reque st.onreadystate change = function(){_thi s.getRequest()} ;
    this.http_reque st.open("POST", url,true);
    this.http_reque st.setRequestHe ader("Content-Type","applicat ion/x-www-form-urlencoded");
    if (document.getEl ementById('text area_url')){
    document.getEle mentById('texta rea_url').value += url + "\n\n" + ((object!=false )?get(document. getElementById( object)):'');
    }
    this.http_reque st.send('xml=<r oot>' + ((object!=false )?get(document. getElementById( object)):'') + '</root>');
    }[/CODE]
    Last edited by acoder; Aug 14 '08, 10:16 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • zamuel
      New Member
      • Sep 2006
      • 42

      #3
      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:

      [code=javascript]
      XHR.prototype.g etRequest = function() {
      if (this.http_requ est!=null) {
      if (this.http_requ est.readyState= =4) {
      if (document.getEl ementById('text area_code')) document.getEle mentById('texta rea_code').valu e = this.http_reque st.responseText ;
      try {
      var output = this.http_reque st.responseXML. documentElement ;
      process(output) ;
      } catch(e) {
      //alert('niet goed');
      }
      }
      }
      }
      [/code]

      Comment

      • zamuel
        New Member
        • Sep 2006
        • 42

        #4
        Hmmmm...tried with code tags, but apparently didn't work.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          I meant the get() function used on lines 19 and 21 in the first post.

          Comment

          • zamuel
            New Member
            • Sep 2006
            • 42

            #6
            Oh yeah now I got it. Here is the code:

            [code=javascript]

            function get(obj) {
            var getstr = "";
            var i = 0;

            if (obj!=false && obj!=null) {
            if (obj.tagName.to UpperCase()!="I NPUT" && obj.tagName.toU pperCase()!="TE XTAREA" && obj.tagName.toU pperCase()!="SE LECT") {
            if (obj.tagName.to UpperCase()!="F ORM") {
            for (i; i<obj.childNode s.length; i++) {
            var oo = obj.childNodes[i];
            if (oo.hasChildNod es() && oo.tagName.toUp perCase()!="TEX TAREA" && oo.tagName.toUp perCase()!="SEL ECT") {
            getstr += get(oo);
            }
            getstr += get_element(oo) ;
            }
            } else {
            for (i; i<obj.elements. length; i++) {
            var oo = obj.elements[i];
            getstr += get_element(oo) ;
            }
            }
            } else {
            getstr += get_element(obj );
            }
            return getstr;
            }
            }
            [/code]

            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.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              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?

              Comment

              • zamuel
                New Member
                • Sep 2006
                • 42

                #8
                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.

                Comment

                • zamuel
                  New Member
                  • Sep 2006
                  • 42

                  #9
                  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.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Is the Console panel in Firebug enabled?

                    Comment

                    • zamuel
                      New Member
                      • Sep 2006
                      • 42

                      #11
                      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!

                      Comment

                      • zamuel
                        New Member
                        • Sep 2006
                        • 42

                        #12
                        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.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          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).

                          Comment

                          Working...