Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8748103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:28:04+00:00 2026-06-13T12:28:04+00:00

I am trying to post the value of the textbox and have that same

  • 0

I am trying to post the value of the textbox and have that same value posted on the page in the “You said…” section.

My TypeScript/JavaScript is:

declare var document;
declare var xmlhttp;

window.onload = () => {
    start();
};

function sayHello(msg: any) {
    // Post to server.
    xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
          // All right - data is stored in xhr.responseText
            alert("done" + " " + xmlhttp.responseText);
        }
        else {
          // Server responded with a status code.
            alert("error");
        }
      }
    }
    xmlhttp.open("POST", "Default.cshtml");
    xmlhttp.send("someValue=" + msg);


    return msg;
}

function start() {
    // Add event Listeners for user interaction
    var element = document.getElementById("link");

    element.addEventListener("click", function () {
        var tb = (<HTMLInputElement>document.getElementById("tbox"));

        var element = document.getElementById("response")
            .innerText = sayHello(tb.value);
    }, false);

    // Setup XMLHttpRequests (AJAX)
    if (XMLHttpRequest) {
        // Somewhat cross-browser
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // Legacy IE
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

And the HTML is (this page is Default.cshtml):

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Home Page";

    var msg = Request["someValue"];
}
<h1>TypeScript HTML App</h1>
<div id="content">
    <a href="javascript:;" id="link">Say Hello</a>:
        <br />
    <input type="text" value="dfgdfgdfg" id="tbox" />
    <br />
    <p id="response">awaiting a response.</p>
    <br />
    <p>You said:<br />
    @msg</p>
</div>

And I’ve included all references properly:
<script src="~/App.js"></script>

The response code I get back is 200.

Am I doing something wrong here? I’ve followed many tutorials, docs and so forth, and I just don’t see what I’m doing wrong. It looks practically identical.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:28:05+00:00Added an answer on June 13, 2026 at 12:28 pm

    When you are processing an XMLHttpRequest as a POST, you need to add a couple of extra headers – add them before you call send, like this:

    var params = "someValue=" + encodeURIComponent(msg);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length.toString());
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    

    UPDATE – My Full Example

    Default.cshtml

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
        Page.Title = "Home Page";
        var msg = Request["someValue"];
    }
    
    @if (msg != null) {
        Layout = null;
        <text>You said @msg</text> 
    } else {
    
        <h1>TypeScript HTML App</h1>
        <div id="content">
            <a href="javascript:;" id="link">Say Hello</a>:
            <br />
            <input type="text" value="dfgdfgdfg" id="tbox" />
            <br />
            <p id="response">awaiting a response.</p>
        </div>
    }
    

    App.ts

    declare var document;
    declare var xmlhttp: XMLHttpRequest;
    
    window.onload = () => {
        start();
    };
    
    function sayHello(msg: any) {
        // Post to server.
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
              // All right - data is stored in xhr.responseText
                //alert("done" + " " + xmlhttp.responseText);
                document.getElementById("response").innerText = xmlhttp.responseText;
            }
            else {
              // Server responded with a status code.
                alert("error");
            }
          }
        }
    
        var params = "someValue=" + encodeURIComponent(msg);
        xmlhttp.open("POST", "");
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length.toString());
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(params);
    
        return msg;
    }
    
    function start() {
        // Add event Listeners for user interaction
        var element = document.getElementById("link");
    
        element.addEventListener("click", function () {
            var tb = <HTMLInputElement>document.getElementById("tbox");
            sayHello(tb.value);
        }, false);
    
        // Setup XMLHttpRequests (AJAX)
        if (XMLHttpRequest) {
            // Somewhat cross-browser
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // Legacy IE
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to customize a script and need to get a POST value
I am trying to use the return value(response) from the callback outside the jQuery.post
I’ve been trying to post geolocation-targeted status updates on a Page through the Graph
Is it possible to post, say like a value in a textbox, to 2
I have the following partial view that I am trying to get to call
I have an issue cropping up in a form I'm trying to post. In
I have the following in a simple form; <form method=post action=inc/core.php> <input type=textbox name=lognick
I have a user control that will render a textbox or file upload. I
Basically all I am trying to do is have a View post back to
I am trying to design a page that handles both Employee and Station CRUD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.