SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I send a large amount of data to a server?

    Hi,
    I am using Ajax to send a large text box to a server script, however, when the textbox has a lot of data it fails. Is there a better way for me to do this?

    Here is my code.

    Code:
    <script type="text/javascript">
    function testmail()
    {
    	
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("results").innerHTML=xmlhttp.responseText;
    	
    	
        }
      }
      
    
    var subject = "subject=" + document.getElementById('subject').value;
    var testemail = "&email=" + document.getElementById('testaddress').value;
    var mbody = "&body=" + document.getElementById('ha').value;
    xmlhttp.open("POST","newmailer.php?" + subject + testemail + mbody ,true);
    xmlhttp.send();
    //alert(qs);
    }
    
    </script>
    Thanks in advance.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Have you checked what the reason for the failure is? This might be easier to fix than to just figure out a different route. You can start by seeing what the response from the server is - it might be some error message.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, yes, I do not get any response when I send a large amount of text. Maybe there is a limit on the amount of data I can send?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try setting the request header's content-type to this:

    Code javascript:
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am a little curious about this line in your code

    Code:
     
    xmlhttp.open("POST","newmailer.php?" + subject + testemail + mbody ,true);
    That would be ok if the method was GET and not POST.

    I thought if using POST you have to send the paramaters separately.

    I use this to send POST ajax requests

    Code:
     
        var url   = "captchaCompare.php";
     
        var params  = "";
     
        params = "txtUserAns="+userAns+"&sid="+Math.random();  
     
        ajaxObj.open("POST",url,true);
     
        ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     
        ajaxObj.send(params);
    I think with GET there is a max number of chars you can send in the query string (not sure what it is off the top of my head).

    Could it be that because you are appending your query string to the url, the request is being sent as a GET even though you have set it as a POST?

    Try sending your query string parameters separately as above and see if that helps.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hah, good catch Kalon. Serves me right for not paying proper attention!

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was easy for me to pick up because I used to make the same mistake

  8. #8
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kalon, great help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •