简体   繁体   English

AngularJs $http post 不发送数据到 restful api

[英]AngularJs $http post not sending data to restful api

This is my angular controller that performs the post function这是我的 angular controller 执行后 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;
        });
    };

You are defining a $http local variable, maybe that is the problem?您正在定义一个$http局部变量,也许这就是问题所在?

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

reference jsfiddle参考jsfiddle

I finally found a way to perform post-function with this controller.我终于找到了用这个 controller 执行后期功能的方法。 I changed this: this.accounts = code: "", account_name: "", description: "," account_type_id:"", is_active:"", user_id:"", owner_id:""};我改变了这个:this.accounts = code: "", account_name: "", description: "," account_type_id:"", is_active:"", user_id:"", owner_id:""}; this.account(ie singular form of accounts) both on the ng-model collecting the information from the frontend and the data passed to http Thanks everyone. ng-model 上的 this.account(即单数形式的帐户)从前端收集信息和传递给 http 的数据谢谢大家。

A singular "s" has been troubling me for some time now.一段时间以来,一个单数的“s”一直困扰着我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM