Send bulk data using Ajax using POST


This post is related to my previous post - A Forgotten Story – Ajax in ASP.NET using JavaScript. So, In case of any doubt, please refer the post. This post explains how to send bulk data from client-side to server-side via Ajax.

Recently, in one of my application, I’s trying to send some XML data from client-side to server-side through Ajax, using the default GET method. As long as the size of the xml data is minimal, it works fine. But, when I tried to send a lengthy data using the GET method, then the parameter/argument to which the data assigned appears to be empty, in the server-side.

A small digging took me here.

This is because in Internet Explorer, the GET method can only send a limited set of data to the server-side and is restricted to 2083 characters. To overcome this limitation we can use POST method for sending huge data to the server-side, in the form of name-value pairs. For this, the XMLHttpRequest has a method: send(args) which accepts arguments, which will be posted/send to the server.

Another thing to note is, since you are sending the request through POST, you cannot use Request.QueryString to get the argument’s value. Instead you’ve to use Request.Form[argument_name].

Last but not the least, you’ve to set the RequestHeader during Ajax call as below, else the value in the parameters will be empty.

   1: xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   2: xmlHttp.setRequestHeader("Content-length", args.length);

The entire code looks like

Client-Side

   1: <script type="text/javascript">
   2:     function sendViaAjax() {
   3:  
   4:         // URL to make the Ajax call
   5:         var url = 'Test.aspx';
   6:  
   7:         // Arguments (with BULK DATA) that is to be passed to the server-side
   8:         var args = 'arg1=somedata&arg2=somebulkdata';
   9:  
  10:         // Creates an Ajax call object
  11:         var xmlHttp = new XMLHttpRequest();
  12:  
  13:         // Specify POST method & URL to connect
  14:         xmlHttp.open("POST", url, true);
  15:  
  16:         // Callback function to response from Server
  17:         xmlHttp.onreadystatechange = function () {
  18:             if (xmlHttp.readyState == 4) {
  19:                 if (xmlHttp.status == 200) {
  20:                     alert(xmlHttp.responseText);
  21:                 }
  22:                 else {
  23:                     alert('Status: ' + xmlHttp.status + ' Response:' + xmlHttp.responseText);
  24:                 }
  25:             }
  26:         };
  27:  
  28:         xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  29:         xmlHttp.setRequestHeader("Content-length", args.length);
  30:  
  31:         // Send Ajax request to Server, along with Arguments
  32:         xmlHttp.send(args);
  33:     }
  34: </script>


Server-Side

   1: protected void Page_Load(object sender, EventArgs e)
   2: {
   3:     string arg1 = Request.Form["arg1"];
   4:     string arg2 = Request.Form["args2"];
   5:     
   6:     // Some operations comes here
   7: }


Yup! That’s it.

Now you know how to send bulk data through Ajax. By the way, this article is aimed at Internet Explorer users.

If you get any error-code like 12030/12031, then please do refer to List of Windows Error Messages.

Hope this helped.

Thanks and Happy Ajaxing.

 

VSAchievements

Visual Studio Achievements
abhilashca (132 Points)
  • Don't Try This At Home
    • Magic Numbers
      Magic Numbers (0 Points)
      Write a enum with 30 fields. Who needs numbers when you've got words! Uses FxCop
      1 days ago
    • Overload
      Overload (0 Points)
      More than 10 overloads of a method. You could go with this or you could go with that. Uses FxCop
      Wednesday, February 13, 2013
    • Go To Hell
      Go To Hell (0 Points)
      Use of the goto keyword. Um, I heard it was a best practice that you weren't supposed to do that anymore.
      Friday, January 04, 2013
    • Scroll Bar Wizard
      Write a single line of 300 characters long. Who needs carriage returns? Uses FxCop
      Thursday, December 27, 2012
  • Power Coder
    • Time For An Upgrade
      A solution takes 10 minutes to compile. Dag, that's one enormous solution!
      Friday, May 03, 2013
    • Suspicious
      Suspicious (10 Points)
      Use 5 preprocessor directives. Compiler commands -- aren't you fancy!
      Friday, February 22, 2013
    • Equal Opportunist
      Equal Opportunist (10 Points)
      Write a class with public, private, protected and internal members. It's all about scope. Uses FxCop
      Monday, January 21, 2013
    • On The Shoulders of Giants
      Reference 25 assemblies. Hey, why should you write it if someone else already did?
      Tuesday, January 08, 2013
    • Localization Guru
      Localization Guru (10 Points)
      Have 1000 localized values. Nice localization work!
      Thursday, January 26, 2012
  • Just For Fun
    • Save A Tree
      Save A Tree (5 Points)
      Print source code. My boss told me to. I swear!
      Monday, March 04, 2013
    • Obsessive Compulsive Disorder (OCD)
      Invoke the 'Close All But This' menu option 10 times. I mean, who invented those damn tabs anyway?
      Sunday, February 26, 2012
    • Lonely
      Lonely (5 Points)
      Code on a Friday or Saturday night. Coding? Tonight? Ouch.
      Saturday, February 04, 2012
    • Install and Register For Visual Studio Achievements
      Install the add-in and register with Channel9. You are up and running!
      Sunday, January 22, 2012
  • Windows 8
  • Unleashing Visual Studio
    • The Explorer
      The Explorer (5 Points)
      Start a debug session using step into (F11) more than 10 times. Ah, the power of F11. Every coder's best friend.
      Monday, February 27, 2012
    • Casual Observer
      Casual Observer (5 Points)
      Start a debug session using step over (F10) more than 10 times. I thank the debugger every day.
      Saturday, February 11, 2012
  • Good Housekeeping
    • Regional Manager
      Regional Manager (7 Points)
      Add 10 regions to a class. Your code is so readable, if I only didn't have to keep collapsing and expanding!
      Thursday, February 02, 2012
  • Customizing Visual Studio
    • Extensions Junkie Deluxe
      Install 10 extensions to Visual Studio. How can you find anything on a menu?
      Sunday, January 22, 2012
    • Extensions Junkie
      Install 5 extensions to Visual Studio. Extensibility rocks!
      Sunday, January 22, 2012

Month List

Tag cloud