The content of this section:
- Ajax operations related issues
- Confirm
Ajax operations related issues
Executing an AJAX call is very common in today's applications, especially in the Spas (Single-page applications single page application), which is almost the only way to communicate with the server. An Ajax call consists of several repeating steps.
At the client, basically, the JavaScript code should provide a URL, arbitrary data and choose a method (Post,get ... ) to perform an Ajax call, it must wait and process the return value, when a call to the server is made, an error can occur (usually a network error), or other service-side error, the service returns a failed response that carries the error message that the client should process or notify the user (an error dialog box can be displayed). If there is no error, the server sends a return data and the client must process it. During the operation, it usually masks or screens the entire screen and displays a message that Ajax is working on until it is complete.
The server-side code gets to a request, executes some server-side code, catches any exceptions, and returns a valid return to the client. If there is an error, an error message may be sent to the client, and if it is a validation error, the server may add a validation issue. If successful, a return value may be sent to the client.
The way ABP
The ABP uses Abp.ajax, which wraps Ajax calls, to handle these steps automatically, and here's an example of an 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 an optional object that you can pass any parameter (it will be validated by jquery's $.ajax way), some default here: DataType: ' JSON ', type: ' POST ', ContentType: ' Application /json ' (So, before we send to the server, we call Json.stringify to convert JavaScript to a JSON string), we can pass the options to Abp.ajax to override these defaults.
Abp.ajax returns promise, so you can write Done,fail,then .... Handler, in this example, we create a simple Ajax request that calls Peoplecontroller's Saveperson operation, in the done handler, We get the ID of the newly created person in the database and display a successful notification (view the notification API). Let's look at 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 the name and age properties, Saveperson is marked as HttpPost, so the default method for Abp.ajax is post. I simplified the implementation of the method, returning only one anonymous object.
This is straightforward, but there are some important things ABP is dealing with in the back, let's go into the details ...
Ajax return information
Even if we return a personid=2 object, the ABP wraps it up as a Mvcajaxresponse object, and the AJAX response essentially looks like this:
{
"success": true,
"result": {
"personId": 42
},
"error": null,
"targetUrl": null,
"unAuthorizedRequest": false,
"__abp": true
}
Here, all the properties are small camel named (because this is a convention in the JavaScript world), even if they are large camel named on the server. Let's explain these properties:
- Success: A Boolean value (True or false) that indicates whether the operation succeeded, if true, Abp.ajax promise and calls the done handler, if False (if there is an exception thrown in the method call), it calls the fail handler and uses the Abp.message.error function to display the error information.
- Result: Returns the results of the operation in the controller, which is only available if success is true when the server sends a return value.
- Error: If success is false, this property contains an object with error details.
- TargetUrl: Provide a URL to the server and, if necessary, direct the client to this URL.
- Unauthorizedrequest: The server gives the client a notification that the operation has not been authenticated or the user has not been authenticated. If the current page is re-loaded for True,abp.ajax.
- _ABP: A special flag that indicates that the response is an ABP wrapper and you don't need to use it, Abp.ajax will handle it.
The Abp.ajax function recognizes and processes this return format, and if not, abp.ajax your done handler gets the return value of the real controller (an object that contains the PersonID property).
Handling Errors
As mentioned above, the ABP handles the exception on the server and returns an object that contains the error message:
{
"targetUrl": null,
"result": null,
"success": false,
"error": {
"message": "An internal error occured during your request!",
"details": "..."
},
"unAuthorizedRequest": false,
"__abp": true
}
As you can see, success is false and result handles this object for Null,abp.ajax and displays an error message to the user through the Abp.message.error function. If the server throws an exception of type userfriendlyexception, it directly displays an error message to the user, otherwise it hides the actual error (writes the error to the log) and displays an "an internal error occurred ..." message to the user, and these ABP are automatically processed.
You might want to suppress the display of information for certain Ajax calls, and you can add abphandleerror:false to the options in Abp.ajax.
HTTP status Code
The ABP returns the given status code for the exception:
- 401 is an unauthenticated request (the user is not logged in, but the service-side operation requires authentication).
- 403 is an unauthorized request.
- 500 for all other types of exceptions.
Wrapresult and Dontwrapresult characteristics
You can control the packaging by using the Wrapresult and Dontwrapresult features for all operations of an operation or controller.
ASP. NET MVC Controller
If the ASP. NET MVC action method return type is Jsonresult (or asynchronous task<jsonresult>), the ABP is wrapped by default (as described above), and you can use the Wrapresult attribute to change this behavior as follows:
public class PeopleController : AbpController
{
[HttpPost]
[WrapResult(WrapOnSuccess = false, WrapOnError = false)]
public JsonResult SavePerson(SavePersonModel person)
{
//TODO: save new person to database and return new person‘s id
return Json(new {PersonId = 42});
}
}
As a shortcut, we can use [Dontwrapresult] only to achieve the same purpose as this example.
You can configure from boot (using CONFIGURATION.MODULES.ABPMVC () ... ) To change this default behavior.
ASP. NET WEB API Controller
ABP does not wrap the results of a successful web API operation by default, and if necessary, you can add wrapresult to an operation or controller, but the default wrapper exception.
You can configure from boot (using Configuration.Modules.AbpWebApi () ... ) To change this default behavior.
Dynamic Web API Layer
ABP wraps the method results of the Dynamic Web API layer by default, and you can change this behavior by using the Wrapresult and Dontwrapresult features on your app service interface.
ASP. NET Core
kid1412 attached: English Original: Http://www.aspnetboilerplate.com/Pages/Documents/Javascript-API/Message
<<ABP documentation >> Javascript Api-ajax