Could someone explain to me why this error is returning to me? (Uncaught Referenceerror: Response is not defined)

Asked

Viewed 963 times

0

inserir a descrição da imagem aqui I have two models (classes)

Class Member

namespace GerenciaIgreja.Models { public class Membro { public Membro() { 
DataCadastro = DateTime.Now; }

public int Id { get; set; }
public DateTime DataCadastro { get; set; }
public string Nome { get; set; }

[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
[Display(Name = "Data de Nascimento")]
public DateTime DataNascimento { get; set; }
[Required]
public string Cpf { get; set; }
[Required]
public string Rg { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
public string Celular { get; set; }
public string Endereco { get; set; }
public bool Ativo { get; set; }


//[Required(ErrorMessage = "Função Ministerial é obrigatório")]
public FuncaoMinisterial FuncaoMinisterial { get; set; }
//public ICollection<Agenda1> Agenda1 { get; set; }

}
}

Class Ministerial office

public class FuncaoMinisterial { public FuncaoMinisterial() { dataCadastro = DateTime.Now; }

public DateTime dataCadastro { get; set; }
public int Id { get; set; }

[Required]
public string Nome { get; set; }
 public virtual ICollection<Membro> Membros { get; set; }
public bool Ativo { get; set; }
}

Now I need to perform a Insert in the database, in the member table. For this I need to pass the normal attributes of the classes member plus the object ministerial function that is behaving as a class attribute member. For this I take an id(value) along with my controller(url) address and pass a method .post of jQuery, to pick up the object official, the problem is there... to catch that blessed object.

  • 1

    The return response; of recupera_funcao_ministerial does not make sense, but it is better to put the place where this function is being used to be easy to propose an alternative solution.

  • The error occurs in the penultimate line pasted here, in the return of function recupera_funcao_ministerial.

  • 1

    @Jeffersonquesado is "kind of hard" (impossible) o return working with asynchronous things :)

  • 1

    Thanks @Jeffersonquesado , vlw for the tip.

2 answers

1

The functions $.ajax, $.get, $.post, $.getJson are all functions asynchronous, because Ajax is an asynchronous request technique (without paging) via Xmlhttprequest API, To use Ajax you need callback, return will never work because Ajax is still being loaded, I recommend you read this:

Once you understand how asynchronous works and the need for callback, let’s adjust your code, you can do something like:

function recupera_funcao_ministerial(valor, url, funcionou, falhou) {

    if (url !="" && valor !="") {

        $.post(url, {Id: valor}, function (response) {
            if (response != null)
            {
                funcionou(response);
            }
        }).fail(function() {
            falhou(response);
        });

    } else {
        falhou('falta uma variavel:' + url + ' ' + valor)}
    }    
}

And at the time of using should do so:

recupera_funcao_ministerial(1, '/minhaurl', function (response) {
    //Faz algo aqui se funcionar
}, function (erro) {
    alert(erro);
});
  • 1

    thanks @guilhermeNascimento, when you get home today I will implement, working or not I give a feedback.

-4

try this and see if it has the 2 variables...

function recupera_funcao_ministerial(valor, url) {

response = null;
if(url !="" && valor !=""){
$.post(url, {Id: valor}, function (response) {
    if (response != null)
    {
        return response;
    }

})}
else {
alert('falta uma variavel:' + url + ' ' + valor)}
return response;
}
  • Thanks, I’ll try here.

Browser other questions tagged

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