Home > Blockchain >  AngularJs $http post not sending data to restful api
AngularJs $http post not sending data to restful api

Time:09-06

This is my angular controller that performs the post function

app.controller ('accountsCtrl', function($scope, $http) {

    this.accounts = { code:"", account_name:"", description:"", account_type_id:"", is_active:"", user_id:"", owner_id:""};

    $scope.addPage = "#!/account";
    $scope.uploadPage = "upload.html";
    $scope.exportPage = "export.html";

   let user_token = local_store("get", "primalfitsUser").token;

    let headers = {
        "Content-Type":'application/x-www-form-urlencoded;charset=utf-8;',
        "Authorization":"Bearer " user_token
    };

    $scope.account_save = function($http) {
        this.accounts.is_active = 1;
        console.log(this.accounts);
        // $http.post(base_api_url "/accounts",$httpParamSerializerJQLike(this.accounts) ,{"headers":headers}).then(
        //         result => console.log(result)
        //     ).catch(error => console.log(error));


          $http({
            url: base_api_url "/accounts",
            method: "POST",
            headers: headers,
            data: {'message' : this.accounts},
            paramSerializer: '$httpParamSerializerJQLike'

        }).then((result) =>{
            $scope.info = result.data.message;
        }, function(error){
            $scope.error = error;
        });
    };

CodePudding user response:

You are defining a $http local variable, maybe that is the problem?

 $scope.account_save = function($http) { // <- remove $http! it comes from the controller itself
        this.accounts.is_active = 1;
        console.log(this.accounts);

reference jsfiddle

CodePudding user response:

I finally found a way to perform post-function with this controller. I changed this : this.accounts = code: "", account_name: "", description: "," account_type_id:"", is_active:"", user_id:"", owner_id:""}; this.account(i.e. singular form of accounts) both on the ng-model collecting the information from the frontend and the data passed to http Thanks everyone.

A singular "s" has been troubling me for some time now.

  • Related