Pass data via Ajax to the MVC Controller ASP.NET CORE C #

-1
Hello, I'm a beginner in programming, and I'm trying to pass data via Ajax to my Controller, to be honest I'm just studying so I do not know for sure to understand the advantages of Jquery for this ...

Following some examples, I did the following:

var nome = document.getElementById("Nome").value;
var sobrenome = document.getElementById("Sobrenome").value;
var email = document.getElementById("Email").value;
var senha = document.getElementById("Senha").value;

    if (nome == null || nome == "") {
        return alert("FALTA INFORMAÇÃO");
    }
    if (sobrenome == null || sobrenome == "") {
        return alert("FALTA INFORMAÇÃO");
    }
    if (email == null || email == "") {
        return alert("FALTA INFORMAÇÃO");
    }
    if (senha == null || senha == "") {
        return alert("FALTA INFORMAÇÃO");
    }

    $.ajax({
        type: 'POST',
        url: '../VerificaDuplicidadeCadastro/Home=email?' + email,            
        //data: email,

Well, I've mentioned the 'date' because in the examples everybody uses this, but I honestly do not understand how it works, so I tried to pass the email through the URL itself, but in my controller I'm getting the null value. >

Follow the controller:

public IActionResult VerificaDuplicidadeCadastro(string email)
    {

        var con = new Conexao();

        con.OpenConnection();

        con.CloseConnection();

        return Json(true);

    }

How can I do this properly?

Thank you.

    
asked by anonymous 18.11.2018 / 15:41

1 answer

0

Hello, good morning.

You can find some information about the advantages and disadvantages of using Ajax in this publication .

When you pass information to the same URL , the term we use is parameter passing via Query String.

In order to pass parameters through Query String, we need to obey the following syntax:

  

URLBase? key1 = value1 & key2 = value2

You can find more information about the syntax of the Query String through the following link: What is the correct syntax for query strings?

That is, in your Ajax call, you are informing the url like this:

$.ajax({
   type: "POST",
   url: "../VerificaDuplicidadeCadastro/Home=email?" + email,            
   //data: email,

Notice that you are reversing the signs in the syntax, where the corret

$.ajax({
   type: "POST",
   url: "../VerificaDuplicidadeCadastro/Home?email=" + email,            
   //data: email,

Take a test and see if it solves the problem. And if it does not solve, let us know that we continue the analysis.

-

And about using data in Ajax calling, you can find more information in the Jquery.AJAX . You will find that you can pass values using the JSON format:

$.ajax({
   type: "POST",
   url: "../VerificaDuplicidadeCadastro/Home",            
   data: { "email": email },
    
18.11.2018 / 16:14