Will AJAX calls work from ionic framework?


#1

Hello, Does Ajax calls works from ionic framework. Inside Controller.js I am trying to make a ajax which is not getting called.

getChanges: function(syncURL, modifiedSince, callback) {
alert('Inside Get changes');
    $.ajax({
        url: syncURL,
        data: {modifiedSince: modifiedSince},
        dataType:"json",
        success:function (data) {
            log("The server returned " + data.length + " changes that occurred after " + modifiedSince);
            callback(data);
        },
        error: function(model, response) {
            alert(response.responseText);
        }
    });

},

I am able to see alert message. After that app does nothing.It is not even throwing an error. Any idea?


#2

You are using jQuery. Ionic is built on top of AngularJS which out of the box only supports a very slimmed down version of jQuery (jQuery lite).

Read up on Angular’s version of what you’re trying to do here:

http://docs.angularjs.org/api/ng/service/$http


#4

To expand upon what has been said above it is possible to use the full version of jquery. You need to add it above where you add angular in your index.html.
I’d advise using the $http service first to see if it does what you want.


#5

Thank You. Seems like I need to use $http. But, I making ajax call in a javascript object which I created.
How to pass $http to Java script object?


#6

Have you looked at the documentation? There is an example in there for a factory that uses $http.


#7

works fine in code we have moved across before changing to use $http so it should work for you.

How/where are you calling getChanges()?


#8

Here is my code

window.dao = {
sync= function(){
----
Here is my ajax call
}
}

Here is my controller
.controller(‘DashCtrl’, function($scope, $http) {

$scope.syncdatabase = function(){
    	   	alert('Sync DB');
    	   	dao.sync( );
    	
    };
}

Now How can I pass $http to Sync method? Thanks for looking in to. Appreciate it.


#9

can some one reply to this? How to make $http ($Ajax) call from javascript object?


#10

Here is my problem, let me know if I miss soem thing here.
I createad a java script object where is perform all operations on local daatabse. Now, when ever I need to get data from server I have to make external call using $http. Now How can I pass $http instance to my java script object?`

window.dao ={
syncURL: "../api/employees",
 initialize: function(callback) {
       


    },
     createTable: function(callback) {
 
        
    },
    dropTable: function(callback) {
        
    },
     Sync: function(callback) {
       $http({
		    url: syncURL, 
		    method: "GET",
		    params: {modifiedSince: modifiedSince}
 		}).success(function(data){
         		alert("The server returned " + data.length + " changes that occurred after " + modifiedSince);
                callback(data);
    		}).error(function(){
        	alert(response.responseText);
    	});
    },

Now can some on eplease tell how to make $http call inside java script object?


#11

Your object should probably be a angularjs service. Then you inject your service into the controller and inject the controller in the html and call some function of the controller.

You should not create your own javascript object. Maybe have a look here: http://viralpatel.net/blogs/angularjs-service-factory-tutorial/

You really should read a angularjs beginner tutorial to get familiar with the concepts. Ionic makes a lot of use of these things. You can find a lot of good short videos here: https://egghead.io/technologies/angularjs