[ASP.NET C#/JavaScript] Simple way to send and receive POST data

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,632
This post is a reminder for me on working with sending and receiving POST data. Key points include:
  • No dependencies on writing <asp:Form /> for instant sending POST data.
  • No dependencies on waiting for user inputs to send/retrieve data from the user client to your server.
  • Fast and simple. Straight to the point.
  • No more GET data sending/receiving. There are a bunch of tutorials out there that shows you how to do this.
  • Added a button to emphasize the importance of function calling. Javascript must have parentheses, and C# must not.

Your initial ASP file (Default.aspx):

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Hello world</title>
    <script type="text/javascript" src="Scripts/test.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <%-- Client Javascript code should have parentheses placed at the end of the function name. --%>
            <%--Server C# code should have no parentheses placed at the end of the function name.--%>
            <asp:Button ID="testButton" Text="Test" OnClientClick="test()" OnClick="serverTest" runat="server" />
        </div>
    </form>
</body>
</html>

Javascript code, "test.js":

Code:
window.onload = function () {
    //Using AJAX and learning about AJAX. Can't ignore it forever.
    var xhr = new XMLHttpRequest();   //xhr for short.

    xhr.open("POST", "/Default.aspx", true);
    xhr.onload = function () {
        if (xhr.status == 200) {
            localStorage.setItem("DW", "AHAHAHAHAHA");   //This doesn't get stored after clicking on the button.
            console.log("This worked so good.");  //This will only appear in the browser's console.
        }
    };
}

function test() {
    console.log("Yahoo!");
}

Your C# code-behind file (Default.aspx.cs):

Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTest {
    public partial class Default : System.Web.UI.Page {
        public string result;

        //Taken from here:
        //https://gist.github.com/leggetter/769688
        protected void Page_Load(object sender, EventArgs e) {
            this.result = "";
            if (Request.InputStream.Length > 0) {
                using (Stream receivedStream = Request.InputStream) {
                    using (StreamReader reader = new StreamReader(receivedStream, Encoding.UTF8)) {
                        result = reader.ReadLine();
                    }
                }
                Debug.WriteLine(result);
            }
        }

        protected void serverTest(object s, EventArgs e) {
            Debug.WriteLine("Google");
        }
    }
}

And that is it.

Use Google Chrome's Web Developer Tools, or Firefox's Inspector to look at the log outputs.

Use Visual Studio's Output tab view to check on the InputStream.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top