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

标签 javascript angularjs angularjs-scope

这是我执行 post 功能的 Angular Controller

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;
        });
    };

最佳答案

您正在定义一个 $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

关于javascript - AngularJs $http post 不发送数据到restful api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73608340/

相关文章:

javascript - 如何使用 Angularjs 执行窗口按键事件来设置 Angular 变量

javascript - 检测阵列阵列

angularjs - 我可以获得 Angular 元素的编译后的 html 吗?

javascript - 访问从 Controller 获取的资源?

angularjs-scope - 结合 ng-repeat 和指令拒绝嵌入的内容访问外部范围?

php - 如何通过javascript用文本框生成数字?

javascript - 固定标题元素在窗口上移动减少

javascript - 重新加载 Express 应用程序不起作用(Angular 前端)

javascript - Angular ng-click 事件

javascript - 以 Angular 获取按钮名称