Performing an AJAX call is frequently used by modern applcations. Especially in SPAs (Single-Page Applications), it's almost the only way of communicating with the server.
An AJAX call consists of some repeating steps:
In client side, basically, javascript code should supply a URL, optionally a data and select a method (POST, GET...) to perfom an AJAX call. It must wait and handle the return value. There may be an error (network error generally) while performing call to the server. There may be some error in server side and server may send an failed response with an error message. Client code should handle this and optionally notify user (may show an error dialog). If no error and server sent a return data, it must also handle it. Also, generally it blocks some (or whole) area of the screen and shows a busy indicator until AJAX operation complete.
Server code should get the request, perform some server-side code, catch any exceptions and return a valid response to the client. In an error situation, it may optionally send an error message to the client. In success, it may send a return data to the client.
ASP.NET Boilerplate automates some of these steps by wrapping AJAX calls with abp.ajax function. An example ajax call:
var newPerson = {
name: 'Dougles Adams',
age: 42
};
abp.ajax({
url: '/People/SavePerson',
data: JSON.stringify(newPerson)
}).done(function(data) {
abp.notify.success('created new person with id = ' + data.personId);
});
abp.ajax gets options as an object. You can pass any parameter that is valid in jQuery's $.ajax method. There are some defaults here: dataType is 'json', type is 'POST' and contentType is 'application/json' (So, we're calling JSON.stringify to convert javascript object to JSON string before sending to the server). You can override defaults by passing options to abp.ajax.
abp.ajax returns promise. So, you can write done, fail, then... handlers. In this example, we made a simple AJAX request to PeopleController's SavePerson action. In the done handler, we got the database id for the new created person and showing a success notification (See notification API). Let's see the MVC controller for this AJAX call:
public class PeopleController : AbpController
{
[HttpPost]
public JsonResult SavePerson(SavePersonModel person)
{
//TODO: save new person to database and return new person's id
return Json(new {PersonId = 42});
}
}
SavePersonModel contains Name and Age properties as you guess. SavePerson is marked with HttpPost since abp.ajax's default method is POST. I simplified method implemtation by returning an anonymous object.
This seams straightforward, but there are some important things behind the scenes that is handled by ASP.NET Boilerplate. Let's dive into details...
Even we directly return an object with PersonId = 2, ASP.NET Boilerplate wraps it by an MvcAjaxResponse object. Actual AJAX response is something like that:
{
"success": true,
"result": {
"personId": 42
},
"error": null,
"targetUrl": null,
"unAuthorizedRequest": false
}
Here, all properties are camelCase (since it's conventional in javascript world) even they are PascalCase in the server code. Let's explain all fields:
Converting your return value to a wrapped AJAX response is possible by deriving from AbpController class. It's recognized and evaluated by abp.ajax function. So, they work as pairs. Your done handler in abp.ajax gets the actual return value of the controller (An object with personId property) if there is no error.
Same mechanism exists in Web API controllers also when you derive from AbpApiController class.
As described above, ASP.NET Boilerplate handles all exceptions in server and returns an object with an error message like that:
{
"targetUrl": null,
"result": null,
"success": false,
"error": {
"message": "An internal error occured during your request!",
"details": "..."
},
"unAuthorizedRequest": false
}
As you see, success is false and result is null. abp.ajax handles this object and shows an error message to the user using abp.message.error function. If your server side code throws an exception type of UserFriendlyException, it directly shows exception's message to the user. Otherwise, it hides the actual error (writes error to logs) and shows a standard ''An internal error occured..." message to the user. All these are automatically done by ASP.NET Boilerplate.
While ASP.NET Boilerplate provides a mechanism to make easy AJAX calls, in a real world application it's typical to write a javascript function for every AJAX call. For example:
//Create a function to abstract AJAX call
var savePerson = function(person) {
return abp.ajax({
url: '/People/SavePerson',
data: JSON.stringify(person)
});
};
//Create a new person
var newPerson = {
name: 'Dougles Adams',
age: 42
};
//Save the person
savePerson(newPerson).done(function(data) {
abp.notify.success('created new person with id = ' + data.personId);
});
This is a good practice but time-consuming and tedious to write a function for every ajax call. ASP.NET provides a mechanism to automatically generate these type of functions for application service methods. Read the dynamic web api layer documentation.