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

Discussion in 'General Members Projects' started by tom_mai78101, Jan 25, 2017.

  1. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    +981 / 4 / -1
    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):

    <%@ 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>
        <form id="form1" runat="server">
                <%-- 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" />
    Javascript code, "test.js":

    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() {
    Your C# code-behind file (Default.aspx.cs):

    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:
            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();
            protected void serverTest(object s, EventArgs e) {
    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.

Share This Page