Send 2 Json objects for $ http in AngularJs

0

I am 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);
}

no controller of 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 passing this way:

<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 that it is only sent to the backend , one of the json , the two is not sent, how do I make the controller method of Angular send both json ?

Debugging submission:

    
asked by anonymous 16.06.2016 / 00:47

2 answers

1

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

  

post (url, date, [config]);

That is, the second object you are sending is being treated as a configuration. But there is a way around this situation, you 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 do the separation when you receive the data on the server.

    
16.06.2016 / 01:59
0

I thank colleagues for the answers. I found in this post what I was looking for: insert the link description here

So, my angle method looks 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. This opens up many possibilities.

    
16.06.2016 / 12:17