2

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;
        });
    };
2
  • How do you know that it's not sending a request? Did you check the network tab?
    – Konrad
    Commented Sep 5, 2022 at 11:05
  • yes i did check the network tab,i console.log the data its getting from the frontend console.log(this.accounts); and it shows it contains data but at the network tab, the rquest object contains empty array
    – phenomT
    Commented Sep 5, 2022 at 12:02

2 Answers 2

1

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

6
  • sorry that was a mistake...i have removed the $http local variable declaration and its still not working
    – phenomT
    Commented Sep 5, 2022 at 12:01
  • @phenomT Please check the network tab if the call is made, I suspect paramSerializer is the problem, could you try removing that? Commented Sep 5, 2022 at 12:02
  • yes, about that av tried removing it, i used paramSerializer when i tried using the content type as "Content-Type":'application/x-www-form-urlencoded;charset=utf-8;
    – phenomT
    Commented Sep 5, 2022 at 12:04
  • @phenomT What about network that, what error are you getting? Commented Sep 5, 2022 at 12:04
  • i have since then change it back to application/json and removed the paramSerializer and it still does not work
    – phenomT
    Commented Sep 5, 2022 at 12:04
1

I finally found a way to perform post-function with this controller. I changed the name of:

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

To 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.