Send 2 Json objects to $http in Angularjs

Asked

Viewed 1,475 times

0

I’m using C#/EF/Angular, in my backend I have the following method, which waits for 02 parameters:

public dynamic save(Entidades.CheckList json, Entidades.CheckList json1)
{
    master = new ClassMaster();
    Entidades.CheckList checkListModel = new Entidades.CheckList();
    CheckListDTO checkListDto = new CheckListDTO();          

    return checkListDto.save(checkListModel,  master.contexto);
}

in the controller angular, I have this method:

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

in my view, I’m going through it like this:

<div class="md-card-content" style="display: none;">
    <div class="uk-width-medium-1-2">
        <label>CNPJ</label>
        <input id="cnpj1" class="md-input" type="text" required name="cli_cod" md-input="" ng-model="cliente.codigo">
    </div>
    <div class="uk-width-medium-1-2">
        <label>CNPJ</label>
        <input id="PBMS1" class="md-input" type="text" required name="cli_nov" md-input="" ng-model="cliente.novartis">                                           
    </div>
    <div class="uk-width-medium-1-2">
        <label>CNPJ</label>
        <input id="User2" class="md-input" type="text" required name="user_cod" md-input="" ng-model="user.codigo">
    </div>
    <div class="uk-width-medium-1-2">
        <label>CNPJ</label>
        <input id="PBMS2" class="md-input" type="text" required name="user_nov" md-input="" ng-model="user.novartis">                                         
    </div>
</div>
<!--ng-click="salvarCliente(cliente)"-->
<div class="uk-clearfix uk-margin-large-top">
    <button class="md-btn md-btn-primary uk-float-right" type="submit" ng-click="Salvar(user,cliente)">Salvar</button>
</div>

The problem is it’s only sent to the backend, one of the json, is not sent both, as I do so that the method of controller of Angular send both json?

Debugging of the shipment: inserir a descrição da imagem aqui

2 answers

1

It is not possible to send 2 objects the way you are doing as the service $http expects some parameters to be sent by default. According to the documentation of Angular, that you can read more here, the method POST wait for these parameters:

post(url, date, [config]);

That is, the second object sent by you is being treated as configuration. But there is a way around this situation, just add the 2 objects within 1 single object, but then you need to do the treatment. Example:

var todosJson = [
    primeiro: json,
    segundo: json1
]

$http.post('http://localhost:55959/CheckList/save', todosJson)

Just remember to separate when receiving the data on the server.

  • Thanks Celsom, axei this link Aki: that solved my life, stay the tip there for colleagues: link

  • @alessandremartins actually the link you showed has the same solution I presented. The difference is that here I am placing more than one array on the same object.

  • Got it Celsom, thanks for the help buddy, let’s go.

0

I thank my colleagues for their answers. I found in this post what I’ve been looking for for for a while: insert link description here

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.

  • This is exactly the same thing that I published in my reply, because my solution is done in stages

Browser other questions tagged

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