How to Get Client-Side Data to the Server

Asked

Viewed 234 times

0

Hello.

I developed an application in . NET CORE and the access page will pick up the geolocation of the client with the coordinates of Longitude and Latitude. However, I made this code using javascript, and the same will run on the client side, I made an implement to fill 2 Label with the information, but I am not able to send these to my server, my idea and store the location (With the client’s knowledge)but it’s not working, I’m not being able to do it.

Below the javascript I’m getting the location, and in sequence as are the labels inside my html, I did not put it all but it is inside a form, I am.

Javascript

<script>
    var x = document.getElementById("lat");
    var y = document.getElementById("lot");

    function getLocation()
    {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 

            y.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function showPosition(position) {
        x.textContent = "Latitude: " + position.coords.latitude;
        y.innerHTML = "Longitude: " + position.coords.longitude;
        <%--Codigo para pegar as cordenadas e setar as mesmas nas Labels --%>
        $("#lblat").text("Latitude: " + position.coords.latitude);
        //$("#lblog").text("Longitude: " + position.coords.longitude);
        $("#lblog").text("Longitude: " +position.coords.longitude);
        $("#endereco").text("Longitude: " +position.coords.longitude);
    }


    $(document).ready(function carregar() {
        getLocation();
        showPosition();
    });
</script>

HTML

<div class="container">
    <div class="col">
        <div class="row">
            <hr />
            <asp:Label ID="Label1" runat="server" Text="Controle de Acesso "></asp:Label>
             <br />
            <asp:Label ID="lblat" runat="server" > </asp:Label>
            <br />
            <asp:Label ID="lblog" runat="server" Text=""> </asp:Label>
            <br />
            <asp:Label ID="lbIp" runat="server" Text=""></asp:Label>
            <br />
            <asp:Label ID="lbHost" runat="server" Text=""></asp:Label>
            <br />
             <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
            <br />
             <asp:Label ID="lboutro" runat="server" Text=""></asp:Label>
            <hr />
            <br />
            <br />
             <asp:Label ID="lat" runat="server" Text=""></asp:Label>
               <br />
             <asp:Label ID="lot" runat="server" Text=""></asp:Label>
            <br />
        </div>
    </div>
</div>

below a print of my application picking up the information, I want to take it to store in a database and use this to compare the client’s access locations, and if you have any access in a different location will perform some action.

inserir a descrição da imagem aqui

  • If you want to send data from the client to the server you need to make a GET or POST request for that. As well as implement the reception of this request on the server for the necessary treatment.

  • What doesn’t make much sense is that you are using ASP.NET components that with the statement Runat="Server", and they have no interaction with the server and its manipulation is being performed only on the client side, via javascript. This is only serving to slow down the rendering of your page and add an unnecessary load on the server side.

1 answer

0


To send the client information to the server, I advise using ajax, since you are working with javascript and also do not need client interaction. The . Net Core does not work with the syntax of "Runat=Server".

This documentation makes it clear how to carry out the procedure:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.