-1

I am trying to use ajax in jquery to submit a form data instead of the $HTTP service in angularJS so i can send files with the form data as well , the issue is that upon success or error i can not perform other operations like showing or hiding elements (ng-show) , there is also one more thing , i have the form data in an object called (newSubscriberInfo) , is there is a way to send the data through ajax call as so ($scope.newSubscriberInfo) instead of passing to the Ajax call an object contains the form elements (var formData = new FormData($('#newSubscriberForm')[0]))??? . here is the code

$scope.saveSubscriber = function (id) {
    if(saveStatus === 'New'){
        $http.post('subscribers/add',$scope.newSubscriberInfo)
        .success(function (data,success) {
            $scope.newSubscriberInfo.serial_number = parseInt(data);
            $scope.subscribers.unshift($scope.newSubscriberInfo);
            $scope.newSubscriberForm = false;
            $scope.newSubscriberInfo = {};
        })
        .error(function (data,error) {
            $scope.connectError = true;
        });
    } else {
        var formData = new FormData($('#newSubscriberForm')[0]);
        console.log(formData);
        $.ajax({
            url: 'subscribers/'+id+'/update',
            type: 'POST',
            data: formData,
            cache: false,
            success : function (data) {
                console.log(data);
                $scope.newSubscriberForm = false;
                $scope.newSubscriberInfo = {};
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(errorThrown);
                $scope.connectError = true;
            },
            contentType: false,
            processData: false
        });
    }
}

3 Answers 3

1

you can use angular-file-upload module to do the file transfer, which does allow transfer of form-data as well along with the files.

see the example here on how to do file-upload-with-form-data

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

3 Comments

but the approach which i use it, post the file to server as soon as it is being selected, that allows user to carry on with filling up the form. so as when the form is submitted its really fast. else on form submit user will have to wait till the files gets copied...
i have used it in a previous project but did not really like it! , why should we use a plugin when we do have XHR2 supporting file uploading now?!
i guess i use it mostly because they are used by many developers, which means most common scenarios are covered and issues are ironed out and i don't have to reinvent the wheel :)
0

If you do things outside of angular and want to update the scope you need to wrap them in $scope.$apply:

$.ajax({ ... })
    .success(function() {
        $scope.$apply(function() {
            // do stuff
        })

    });

See here for more details

1 Comment

thanks , do you have an idea how can i pass the form data as such ($scope.someObject) to the AJAX call instead of passing to the Ajax call an object contains the form elements (var formData = new FormData($('#someForm')) ?
0

Your async response is not in the angular world. To make it you can either use $scope.$apply inside your callbacks, or better yet wrap the jQuery promise in an angular promise. Something like this:

    $q.when($.ajax({
        url: 'subscribers/'+id+'/update',
        type: 'POST',
        data: formData,
        cache: false,
        contentType: false,
        processData: false
    })).then(function (data) {
            console.log(data);
            $scope.newSubscriberForm = false;
            $scope.newSubscriberInfo = {};
        },
        function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(errorThrown);
            $scope.connectError = true;
        });

3 Comments

Hmm.. the arguments to the error handler is probably a bit different. But the example should convey the general tecnique.
the answer by Dean Ward really worked fine , i had to use "apply" as ajax is outside the angular scope!
I know his answer works. I merely thought I'd give you another alternative. Programming isn't about finding a solution, it's about finding choosing the better solution. I'm not saying that this one is better, but knowing your options is a prerequisite to making an informed choice.

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.