Dipak Dipak - 1 year ago 155
AngularJS Question

how to make synchronous http request in angular js

how to make blocking http request in angular js so that i can use the $http response on very next line in angular js.

here $http object doesn't return the result on next line so that i can pass the $http response on fullcalender a javascript library.

here $scope.data returns blank value.

Sample code below


$scope.data = data;


data: $scope.data

Answer Source

You can use promises for that.

here is an example:

$scope.myXhr = function(){

    var deferred = $q.defer();

        url: 'ajax.php',
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        //if request is successful

            //resolve the promise
            deferred.resolve('request successful');

        //if request is not successful
            //reject the promise

    //return the promise
    return deferred.promise;

$scope.callXhrAsynchronous = function(){

    var myPromise = $scope.myXhr();

    // wait until the promise return resolve or eject
    //"then" has 2 functions (resolveFunction, rejectFunction)
        }, function(reject){

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download