ASP.NET read data from a POST ajax

Asked

Viewed 32 times

0

Below is my "form" I want to send to the server in an ajax post

$('#ajaxButton').on('click', function () {
            const ajaxFormData = {
                nome: $('#nome').val(),
                zap: $('#whatsapp').val(),
                email: $('#email').val()
            };

            $.ajax({
                url: "/plano-detalhes.aspx",
                data: ajaxFormData,
                type: "POST",
            });
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ajaxForm">
                <h2>Negociar Valores</h2>
                <label>
                    Nome
                    <input id="nome" type="text"/>
                </label>
                <label>
                    Whatsapp
                    <input id="whatsapp" type="text" />
                </label>
                <label>
                    e-mail
                    <input id="email" type="text"/>
                </label>

    <button id="ajaxButton">Enviar</button>
 </div>

The problem is that I don’t know how to collect this data sent on the aspx page, see what I’m trying below:

Request r = new Request();
if (r.isPost)
    {
        try
        {
            dictionary<string, string> ajaxData = new Dictionary<string, string>();
            foreach(KeyValuePair<string, string> n in r.data)
            {
                ajaxData.Add(n.Key, n.Value);
            }
            //apenas para eu tentar ler
            System.Diagnostics.Debug.Write(ajaxData.toString());
        }
        catch(Exeption e)
        {
           System.Diagnostics.Debug.Write(e);      
        }
    }

I’m new in C# ASP.NET, I came from a javascript background, I couldn’t find any way to read the data I’m sending.

thank you in advance

  • What are you using? webforms or mvc?

  • webforms, and in this case I can not send directly through the normal form, if not from the refresh on the page and, I’m picking up some data from the previous page.

1 answer

1


First you have to enable on the page or on the master page the page methods:

<asp:ScriptManager ID="scriptmanager" runat="server" EnablePageMethods="true" />

Then create a class to represent the object:

 public class Pessoa {
     public string nome {get;set;}
     public string zap {get;set;}
     public string email {get;set;}
 }

And then on the page create the web method:

[WebMethod]
public static string GravarPessoa(Pessoa pessoa)
{
   var nome = pessoa.nome
}

In the post address must have the method name:

 url: "/plano-detalhes.aspx/GravarPessoa"

Remembering that the web method is static

Browser other questions tagged

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