0

i am start to learn about angular js. I have a javascript function which is post function, now i want to convert it to angular js.

Here is my old javascript code:

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

I can get the return result from this old javascript, it return me the json result. Below is my new angular js function which couldnt works. I not sure whether i need to change the backend code.

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

I wish someone to tell me what i less to convert into angular js. Thanks.

7
  • 1
    You have not specified the action in url: 'Software.aspx', Commented May 24, 2017 at 7:01
  • check your console log Commented May 24, 2017 at 7:04
  • What does not work? Do you have any HTTP status in result? Commented May 24, 2017 at 7:09
  • @doncadavona It is Angular 1, look at the code and the tag angularjs. Commented May 24, 2017 at 7:10
  • Oh yeah I see it's angularjs. Commented May 24, 2017 at 7:13

1 Answer 1

1

I found out this method could achieve the same result from the 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")

            });

        };

Need to set the headers to this:

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

Then only this will works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.