将javascript post函数转换为angular js

时间:2017-05-24 06:59:13

标签: javascript jquery angularjs

我开始学习角度js。我有一个javascript函数,它是post函数,现在我想把它转换为angular js。

这是我的旧javascript代码:

<script>
function submitFunction() {
    var ip_range = "";

    var user_name = "wind";

    $.post("software.aspx", {"action": "getchartdata","username": user_name},
        function(data, status) {

            if (status === "success") {
                if (data) {
                    console.log(JSON.stringify(data));

                    dataParse(data);
                } else {
                    alert("ALERT!");
                }

                alert(start_date)
            }
            alert(start_date)


        });
}
</script>

我可以从这个旧的javascript获得返回结果,它返回json结果。下面是我的新角度js函数,它无法工作。我不确定是否需要更改后端代码。

<script>
    var app = angular.module('myApp', []);
    app.controller('FormCtrl', function ($scope, $http) {

        console.log("Preparing..")
        $scope.submitForm = function () {
            console.log("posting data....");
            var username = $scope.form;
            $http({
                method: 'POST',
                url: 'Software.aspx',
                data: {username: username},


            }).then(function successCallback(response) {
                // this callback will be called asynchronously
                // when the response is available
                console.log("Success")
                console.log(response)

            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.log("fail")

            });

        };
    });
</script>

我希望有人能告诉我我转换成角度js的意义。感谢。

1 个答案:

答案 0 :(得分:1)

我发现这种方法可以从javascript中获得相同的结果。

 $scope.submitForm = function () {
            console.log($scope.selected)

            console.log(startDate)
            $http({
                method: 'POST',
                url: 'Software.aspx',
                data: {
                    "action": "getchartdata",
                    "username": $scope.username,

                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                transformRequest: function (data) {
                    return $.param(data);
                }
            }).then(function successCallback(response) {
                // this callback will be called asynchronously
                // when the response is available
                console.log(response);
                dataParse(response.data);


            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.log("fail to send")

            });

        };

需要将标题设置为:

 headers: {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                    },
                    transformRequest: function (data) {
                        return $.param(data);
                    }

然后这只会有效。