Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am beginner in java script and does not understand how to create synchronous Ajax call using $http object if anybody have idea please guide me, how i can make Ajax call by $http sychronously

my code as follow -

var AjaxModule = angular.module('AjaxModule',[]);
AjaxModule.controller('AjaxController',function($scope,$http){
    var path ="http://localhost/services_ajax/";
    var serviceName = 'customers'; 
    var response = $http.get(path+serviceName);
    response.success(function(data){
        $scope.list = data;
    });
});
share|improve this question
1  
Why do you think you would need a synchronous-jax call? –  Bergi yesterday
2  
The $http service returns a promise, therefore I don't think that you can configure it to make a synchronous call... However, I can't think of a single case where I would want my app to stop until I get a response from the server. –  Josep yesterday
    
becouse i have set DataTable rows by iterating $scope.list in html all html generate properly but DataTable search is not working as dataTable call before generating the table rows due asynchronous ajax request –  Niles yesterday
1  
So disable the search until data are loaded. –  Blackhole yesterday

2 Answers 2

up vote 1 down vote accepted

you can use the promise concept of angular. promise provide the synchronous facility. i demonstrate you to by giving the demo example

var app = angular.module("myApp",[ ]);

app.controller("ctrl",function($scope,$q,$timeout){

var task1 = $q.defer();
task1.promice.then(function(value){
       // write a code here for your task 1 success
} ,function(value){
       // write a code here for your task 1 error
});

var task2 = $q.defer();
task2.promice.then(function(value){
      // write a code here for your task 2 success
} ,function(value){
     // write a code here for your task 2 error
});

$q.all([task1.prpmice,task2.promice])
     .then(function(){
             // write a code which is executed when both the task are completed
    } ,function(){
            // write a code which is executed when some of the task are rejected
});

}

the above code will help you to understand the promice concept of angular

share|improve this answer

You can not make a synchronous request using $http service. It is hard coded to be asynchronous in the service code. You can, however, make your own synchronous service.

var myApp = angular.module('myApp', []);

myApp.service('synchronousService', [function () {
    var serviceMethod = function (url) {
        var request;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            throw new Error("Your browser don't support XMLHttpRequest");
        }

        request.open('GET', url, false);
        request.send(null);

        if (request.status === 200) {
            return request.responseText;
        }
    };
    return serviceMethod;
}]);

myApp.controller('AppCtrl', function ($scope, synchronousService) {
    var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22AAPL%22%29&env=store://datatables.org/alltableswithkeys";
    alert(synchronousService(url));
});

Here is working jsfiddle: http://jsfiddle.net/zono/uL0e1j3e/18/

Just to say that the synchronous request is a very bad idea.

share|improve this answer
2  
I'm not sure that this service will work properly across all different browsers. –  Josep yesterday
    
This is just a example. I will improve answer. –  Georgi Naumov yesterday
    
The answer is edited. –  Georgi Naumov yesterday
1  
Still, that wouldn't work with IE9 (for example). The real deal here is that the OP needs to understand that Angular doesn't provide that option for a reason, which is that Angular apps are meant to work with async functions and promises. –  Josep yesterday
    
I never said that this will work for old IE. Does not matter. I improved it. I already says that the synchronous request is a very bad idea. Edit: Whys is this down vote? –  Georgi Naumov yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.