jQuery promise object based implementation for ajax

jQuery promise object based implementation of the ajax call

$.ajax().done().error();

Parameters
$.ajax({

url:
type: ‘GET’|’POST’|’PUT’|’DELETE’,
data:,
datatype:’json|xml|html|bin|img’,
contenttype:’application/json;utf-8’|’application/xml’,
headers:{
‘AUTHORIZATION’, ‘basic_’+bton(username:password) OR
‘AUTHORIZATION’, ‘bearer_’ + TOKEN
}

});

//Create Service Layer for Ajax calls

var promise = $.ajax();’

//Create a Call Layer (ViewModel)
Use the Promise Object and Write Success and Fail Callback

promise.done();

promise.error();

Example :

Add below code to Service.js

this.getEmployees = function () {
var promise = $.ajax({
url: url,
type:’GET’
});
return promise;
};

Use of Promise object

var servObj = new service();

function loadData() {
var promise = servObj.getEmployees();
promise.done(function (resp) {
self.Employees(resp);
self.Message(‘Call Completed Successfully….’);
});
promise.error(function (err) {
self.Message(‘Error ‘ + err.status);
});
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s