简体   繁体   中英

AngularJS HTTP GET request not working

I have a code

angular.module('ngMailChimp', ['ngAria', 'ngMessages', 'ngAnimate'])
    .controller('SignUpController', function () {
        var ctrl = this,
            newCustomer = { email:'', userName:'', college:'' },
            actions,
            MailChimpSubscription;

        var signup = function () {
            if( ctrl.signupForm.$valid) {
                $http({
                    url: 'http://' + 'campusconnect' + '.' + 'us11' +'.list-manage.com/subscribe/post-json', 
                    method: "GET",
                    params: {NAME: ctrl.newCustomer.userName,
                            COLL : ctrl.newCustomer.college,
                            EMAIL : ctrl.newCustomer.email,
                            u : "35f503a1404877769e67c22f9",
                            id : "d5a2aab2f9"                           }
                });




                //MailChimpSubscription.save(
        // Successfully sent data to MailChimp.
                //function (response) {
                //    if (response.result === 'error')
                //    {
                //        ctrl.showSubmittedPrompt = false;
                //    }
                //    else
                //    {
                //        ctrl.showSubmittedPrompt = true;
                //        clearForm();
                //    }
                //},
                //function (error) {
                //    $log.error('MailChimp Error: %o', error);
                //}
                //);  
                ctrl.showSubmittedPrompt = true;
                clearForm();        
            }
        };

        var clearForm = function () {
            ctrl.newCustomer = { email:'', userName:'', college:'' }
            ctrl.params={}
            ctrl.signupForm.$setUntouched();
            ctrl.signupForm.$setPristine();
        };

        var getPasswordType = function () {
            return ctrl.signupForm.showPassword ? 'text' : 'password';
        };

        var toggleEmailPrompt = function (value) {
            ctrl.showEmailPrompt = value;
        };

        var toggleUsernamePrompt = function (value) {
            ctrl.showUsernamePrompt = value;
        };

        var toggleCollegePrompt = function (value) {
            ctrl.showCollegePrompt = value;
        };

        var hasErrorClass = function (field) {
            return ctrl.signupForm[field].$touched && ctrl.signupForm[field].$invalid;
        };

        var showMessages = function (field) {
            return ctrl.signupForm[field].$touched || ctrl.signupForm.$submitted
        };

        ctrl.showEmailPrompt = false;
        ctrl.showUsernamePrompt = false;
        ctrl.showCollegePrompt = false;
        ctrl.showSubmittedPrompt = false;
        ctrl.toggleEmailPrompt = toggleEmailPrompt;
        ctrl.toggleUsernamePrompt = toggleUsernamePrompt;
        ctrl.toggleCollegePrompt = toggleCollegePrompt;
        ctrl.getPasswordType = getPasswordType;
        ctrl.hasErrorClass = hasErrorClass;
        ctrl.showMessages = showMessages;
        ctrl.newCustomer = newCustomer;
        ctrl.signup = signup;
        ctrl.clearForm = clearForm;
    })
    .directive('validatePasswordCharacters', function () {
        return {
            require: 'ngModel',
            link: function ($scope, element, attrs, ngModel) {
                ngModel.$validators.lowerCase = function (value) {
                    var pattern = /[a-z]+/;
                    return (typeof value !== 'undefined') && pattern.test(value);
                };
                ngModel.$validators.upperCase = function (value) {
                    var pattern = /[A-Z]+/;
                    return (typeof value !== 'undefined') && pattern.test(value);
                };
                ngModel.$validators.number = function (value) {
                    var pattern = /\d+/;
                    return (typeof value !== 'undefined') && pattern.test(value);
                };
                ngModel.$validators.specialCharacter = function (value) {
                    var pattern = /\W+/;
                    return (typeof value !== 'undefined') && pattern.test(value);
                };
                ngModel.$validators.eightCharacters = function (value) {
                    return (typeof value !== 'undefined') && value.length >= 8;
                };
            }
        }
    })
;

However, On debugging, it doesnt budge ast this line. Whats is the error????

the ctrl.newCustomer is a valid object and I am able to get the strings from my HTML page.

NAME - text input COLL - text input EMAIL - email input

Validation is taken care of

You must inject the $http service:

angular.module('ngMailChimp', ['ngAria', 'ngMessages', 'ngAnimate'])
    .controller('SignUpController', function ($http) {
        ...

Please notice that this form is not safe for minimization, in that case you should use:

.controller('SignUpController', ['$http', function ($http) {
    // your code
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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