Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
1. My Html

HTML
<html>
<form class="form-horizontal" name="HnForm" novalidate>
    <h1>Hostname:</h1>
    <p id="hostname"></p>
    <button type="button" onclick="sendHostname()">Submit</button>
</form>

</html>



    function sendHostname() {
    var hostname = document.getElementById("hostname").innerText;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/HN/GetHostname", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onload = function () {
        if (xhr.status === 200) {
            var response = xhr.responseText;
            console.log("Response from server:", response);
        }
    };
    xhr.send("hostname=" + encodeURIComponent(hostname));
}


2. My controller
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;

namespace HN.Controllers
{
    public class HNController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetHostname(string hostname)
        {
            return Content("Hostname received successfully", hostname.ToString());
        }
    }
}


What I have tried:

My Question why my hostname is null value?

Please help me
Posted
Updated 6-Jun-23 7:15am
v3

1 solution

The HTML contains:
HTML
<p id="hostname"></p>
<button type="button" onclick="sendHostname()">Submit</button>


The Javascript has:
JavaScript
function sendHostname() {
var hostname = document.getElementById("hostname").innerText;

You have a blank paragraph with the id of "hostname", but it does not contain any innerText.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900