doubts with ajax and angular

2

I have my backend, in my controller I have a method

salvarBairro(string dados)

I'm using angular, this guy here calls my save methodBall of the back

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

            });

My back method can only capture the json sent if the parameter is of type NeighborhoodModel, eg:

salvarBairro(BairroModel dados)

If the parameter is of type string, it receives null, even the view sending json. But I need to force the method to get a string, because in addition to the neighborhood data, I want to send out some data, is it possible?

  • How could you do to send more than one json?
  • how could I force the backend method to get json sent, declaring the parameters as string - saveBairro (string data)
asked by anonymous 31.12.2015 / 10:20

3 answers

1

As you are using C # I can not help you 100% but I will respond as I do something similar with Java+Vraptor maybe I can help you.

Controller on the backend:

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

Notice that I'm passing a direct attribute on URL and getting it as a parameter in my listarTodos method

So on 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 concatenate the attribute to URL , different from when I pass an object

    
31.12.2015 / 11:38
1

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

But putting this observation aside to get what you want, you first have to transform the JSON object into a string in Javascript JSON.stringify(bairro) motif the form that will be made the request for the HTML form pattern and pass it on the request .

Try the following 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 ActionResult method to this:

salvarBairro(string dados)
    
31.12.2015 / 13:51
1

I thank colleagues for the answers. I found in this post what I was looking for: various parameters

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:21