[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,260
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.
  • V-SNES V-SNES:
    Happy Sunday!
    +1
  • ToshibaNuon ToshibaNuon:
    Happy sunday!
    +2
  • The Helper The Helper:
    And its Friday!
  • The Helper The Helper:
    Happy Saturday!
    +1
  • V-SNES V-SNES:
    Happy Saturday!
  • The Helper The Helper:
    Happy Monday!
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    Happy Friday!
    +1
  • tom_mai78101 tom_mai78101:
    Starting this upcoming Thursday, I will be in Japan for 10 days.
  • tom_mai78101 tom_mai78101:
    Thursday - Friday will be my Japan arrival flight. 9 days later, on a Sunday, will be my return departure flight.
    +2
  • The Helper The Helper:
    Hope you have safe travels my friend!
    +1
  • vypur85 vypur85:
    Wow spring time in Japan is awesome. Enjoy!
  • The Helper The Helper:
    Hopefully it will be more pleasure than work
  • vypur85 vypur85:
    Recently tried out ChatGPT about WE triggering. Wow it's capable of giving a somewhat legitimate response.
  • The Helper The Helper:
    I am sure it has read all the info on the forums here
  • The Helper The Helper:
    i think triggering is just scripting and chatgpt is real good at code
  • vypur85 vypur85:
    Yeah I suppose so. It's interesting how it can explain in so much detail.
  • vypur85 vypur85:
    But yet it won't work.
  • The Helper The Helper:
    it does a bad ass job doing excel vba code it has leveled me up at my job when I deal with excel that is for sure
  • vypur85 vypur85:
    Nice! I love Excel coding as well. Has always been using Google to help me. Maybe I'll use ChatGPT next time when I need it.
  • The Helper The Helper:
    yeah whatever it puts out even if it is not perfect I can fix it and the latest version of chatgpt can create websites from pictures it will not be long until it can do that with almost all the tools
    +1
  • The Helper The Helper:
    These new Chat AI programs are going to change everything everyone better Buckle the Fuck Up!
  • The Helper The Helper:
    oh and Happy Tuesday Evening! :)
    +1

    The Helper Discord

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top