Angular http method: 'POST' is not sending the $Scope object

Asked

Viewed 73 times

0

I’m having a problem in the angular http method post, I’ve seen several tips but none works. Follow the code:

$scope.cadastrar = function () {
   $http({
        method: 'POST',
        url: '/MinhaConta/Cliente/PostAnuncio',
        data: $.param($scope.novo),
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    })
    .success(function (data, status, headers, config) {
        if (data.Status) {
           $scope.hasMsg = true;
           $scope.hasError = false;
           $scope.hasSuccess = true;
           $scope.infoMsg = data.Mensagem;
        }
    })
    .error(function (data, status, headers, config) {
        $scope.hasMsg = true;
       $scope.hasSuccess = false;
       $scope.hasError = true;
       $scope.infoMsg = data.ErrorMessage;
    });
};

I put the scope to display the information of my object as I insert:

Testando no Browser o bind do modelo

I inserted a breakpoint to see if the object has value

Breakpoint

Response ends up returning me an error which is a default return if the object is null. Would anyone know to tell me what I’m doing wrong?

{ "tipoid": 0, "marcaid": 0, "modeloid": 0, "versaoid": 0, "anofabrication": 0, "anomodel": 0, "km": 0, "value": 0.0, "colour": null, "fuel": null, "plate": null, "vscustom": null, "exchange": false }

public class NovoVeiculo
{
    public int tipoid { get; set; }
    public int marcaid { get; set; }
    public int modeloid { get; set; }
    public int versaoid { get; set; }
    public short anofabricacao { get; set; }
    public short anomodelo { get; set; }
    public int km { get; set; }
    public decimal valor { get; set; }
    public string cor { get; set; }
    public string combustivel { get; set; }
    public string placa { get; set; }
    public string vscustom { get; set; }
    public bool troca { get; set; }
}

Thanks in advance for the help.

  • tries to pass the date variable differently. example: $http({ method: 'POST', url: '/Minhaconta/Client/Postanuncio', date: {new: $Scope.new)}, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })

  • 2

    It would be nice to [Dit] and show the content of $scope.novo

  • so I tried to pass other ways but the resquest in the browser console shows me new{ [] }

  • Without more information I would say that the problem lies in the hierarchy of scope - perhaps the property novo is being created in a sub-scope. At startup controller where you declare $scope.cadastrar, enter '$Scope.new = null' so that the variable is initialized in the correct scope.

  • @Even though you’re right, as the $Cope.new statement was there at the beginning I forgot that I initialized it as an array... it worked now. Thanks for the observation.

  • 1

    @Angélicaflausino good that it worked! To help future users with the same problem, answer with your solution and mark as accepted. And not for that, it’s always a pleasure to help.

Show 1 more comment
No answers

Browser other questions tagged

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