doubt with ajax and angular

Asked

Viewed 310 times

2

I have my back-end, in my controller I have a way

salvarBairro(string dados)

I’m using angular, this guy here calls my way to salvaBairro from the back

 $scope.AddBairro = function (bairro) {
            $http.post("http://localhost:23714/Bairro/save", bairro).success(function (data) {

            });

my back method can only capture the sent json if the parameter is of type Bairromodel, ex:

salvarBairro(BairroModel dados)

if the parameter is of the string type, it takes null, even the view sending json. But I need to force the method to receive a string, because in addition to the neighborhood data, I want to send other data, it is possible that?

  • how could I send more than one json ?
  • how could force the back-end method to receive the sent json, declaring the parameters as string - save)
  • Ta using which language in the back-end?

  • c# / mvc - with Entity framework

  • 1

    I get it, since I don’t know much about technology I’ll tell you how I do it using java, maybe then it’ll be easier for you to understand.

3 answers

1

As you are using C# I can’t help you 100% however I will respond the way I do something similar with Java+Vraptor maybe I can help you.

Controller in the back-end:

@Get
@Path("/listar/{paginaInicio}")
public void listarTodos(Integer paginaInicio) {
    result.use(Results.json()).withoutRoot().from(grupoDAO.listar(paginaInicio, 5)).serialize();
}

Notice that I am passing a direct attribute on URL and receiving it as a parameter in my method listarTodos

So at the front end I have the following service:

 var _getGroups = function(paginaInicio){
    return $http.get(config.baseURL + "/EtiquetaAPI/grupo/listar/" + paginaInicio);
  };

In this service I grant the attribute to URL, different from when I pass an object

  • I will try to do this, but when I have to pass a very big data Qtde, it would not be impossible to pass the url?

  • certainly, I’ll see if I can find another way to pass this data

  • Try to do this: $http.post("http://localhost:23714/Bairro/save", JSON.stringify(bairro))

  • see if it works

  • did not work... receives null

  • I made that video, if you can see ... https://www.youtube.com/watch?v=PWJM6zpFRUg

Show 1 more comment

1

At first in my opinion . NET helps in this part by converting the JSON object into C# object automatically so the BairroModel or another class works. Basically you are giving up a structured information (server-side object) for a less structured (querystring text). Finally this solution is only increasing the complexity of your solution.

But putting this observation aside to get what you want, first Voce has to turn the JSON object into a string in Javascript JSON.stringify(bairro) motif the way the request will be made for the Htmle form pattern to pass it on the request.

Try as follows Angularjs:

$http({
    url: 'http://localhost:23714/Bairro/save',
    method: 'POST',
    data: { dados: JSON.stringify(bairro) },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})

In Your MVC adapt your method ActionResult for that reason:

salvarBairro(string dados)
  • Rogerio, it didn’t work, see in this video https://www.youtube.com/watch?v=yff0snARJqQ regarding your observation, I thought about creating an auxiliary class, and all my models would inherit this class, q vc Axa ?

  • Actually when I use MVC, I do all the classes and call them Request at the end for example. BairroRequest and this class represents only the values that will go in the POST, this way it would be very simple to work, vboce passes the direct object through the angular, without having to transform it into string

  • In your video, Voce calls the input variable in the acesso, so your querystring variable should be acessoand not dados, that is to say: { acesso: JSON.stringify(bairro) }

1

I thank my colleagues for their answers. I found in this post what I’ve been looking for for for a while: various parameters

So my angular method was like this:

$scope.Salvar = function (checklist) {      
$http.post("http://localhost:55959/CheckList/save", { checklist: checklist, parametro1: "parametro1" })
    .success(function (data) {
        if (data == "200") {
            alert("Cadastro realizado com sucesso");
        }
        else
            alert(data);
    })
    .error(function (error) {
        alert("Erro");
    });

};

In this way, it is possible to pass several parameters, in my example there, my first parameter is a json, which in my backend will be automatically converted to Checklist type, the second parameter is a string. That opens up a lot of possibilities.

Browser other questions tagged

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