Skip to content

Commit 93c4d7d

Browse files
authored
Merge pull request #589 from aspnetboilerplate/pr/2826
add html encode to ajax responses
2 parents 059df3f + b691fdb commit 93c4d7d

File tree

7 files changed

+137
-48
lines changed

7 files changed

+137
-48
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/Views/Shared/Layout/_Scripts.cshtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
@*admin-lte scripts start*@
1010
<script src="~/libs/bootstrap/dist/js/bootstrap.bundle.js" asp-append-version="true"></script>
1111
<script src="~/libs/datatables/js/jquery.dataTables.min.js" asp-append-version="true"></script>
12+
<script src="~/view-resources/Views/_Bundles/helpers.min.js" asp-append-version="true"></script>
13+
<script src="~/view-resources/Views/_Bundles/datatables.ajax.min.js" asp-append-version="true"></script>
1214
<script src="~/libs/datatables/js/dataTables.bootstrap4.min.js" asp-append-version="true"></script>
1315
<script src="~/libs/datatables/js/dataTables.responsive.min.js" asp-append-version="true"></script>
1416
<script src="~/libs/datatables/js/responsive.bootstrap4.min.js" asp-append-version="true"></script>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/bundleconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,17 @@
134134
"inputFiles": [
135135
"wwwroot/view-resources/Views/Shared/Components/TenantChange/_ChangeModal.js"
136136
]
137+
},
138+
{
139+
"outputFileName": "wwwroot/view-resources/Views/_Bundles/datatables.ajax.min.js",
140+
"inputFiles": [
141+
"wwwroot/Common/datatables.ajax.js"
142+
]
143+
},
144+
{
145+
"outputFileName": "wwwroot/view-resources/Views/_Bundles/helpers.min.js",
146+
"inputFiles": [
147+
"wwwroot/Common/helpers.js"
148+
]
137149
}
138150
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/************************************************************************
2+
* Ajax extension for datatables *
3+
*************************************************************************/
4+
(function ($) {
5+
if (!$.fn.dataTableExt) {
6+
return;
7+
}
8+
9+
var doAjax = function (listAction, requestData, callbackFunction, settings) {
10+
var inputFilter = {};
11+
12+
//set table defined filters
13+
if (listAction.inputFilter) {
14+
inputFilter = $.extend(inputFilter, listAction.inputFilter());
15+
}
16+
17+
//set paging filters
18+
if (settings.oInit.paging) {
19+
inputFilter = $.extend(inputFilter, {
20+
maxResultCount: requestData.length,
21+
skipCount: requestData.start
22+
});
23+
}
24+
25+
//execute ajax function with filter
26+
if (listAction.ajaxFunction) {
27+
listAction.ajaxFunction(inputFilter)
28+
.done(function (result) {
29+
//store raw server response for custom rendering.
30+
settings.rawServerResponse = result;
31+
32+
//html encoding can be disabled by adding "disableResponseHtmlEncoding: true" to "listAction" field
33+
var dataItems;
34+
if (listAction.disableResponseHtmlEncoding) {
35+
dataItems = result.items;
36+
} else {
37+
//HTML encodes the response items for XSS protection.
38+
dataItems = app.htmlUtils.htmlEncodeJson(result.items);
39+
}
40+
41+
//invoke callback
42+
callbackFunction({
43+
recordsTotal: result.totalCount,
44+
recordsFiltered: result.totalCount,
45+
data: dataItems
46+
});
47+
})
48+
.always(function () {
49+
abp.ui.clearBusy(settings.nTable);
50+
});
51+
}
52+
}
53+
54+
if (!$.fn.dataTable) {
55+
return;
56+
}
57+
58+
$.extend(true, $.fn.dataTable.defaults, {
59+
ajax: function (requestData, callbackFunction, settings) {
60+
if (!settings) {
61+
return;
62+
}
63+
64+
if (!settings.oInit) {
65+
return;
66+
}
67+
68+
if (!settings.oInit.listAction) {
69+
return;
70+
}
71+
72+
abp.ui.setBusy(settings.nTable);
73+
74+
doAjax(settings.oInit.listAction, requestData, callbackFunction, settings);
75+
}
76+
});
77+
78+
$.fn.dataTable.Api.register('ajax.reloadPage()', function () {
79+
// user paging is not reset on reload. https://datatables.net/reference/api/ajax.reload()
80+
this.ajax.reload(null, false);
81+
});
82+
})(jQuery);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var app = app || {};
2+
(function () {
3+
app.htmlUtils = {
4+
htmlEncodeText: function (value) {
5+
return $("<div/>").text(value).html();
6+
},
7+
8+
htmlDecodeText: function (value) {
9+
return $("<div/>").html(value).text();
10+
},
11+
12+
htmlEncodeJson: function (jsonObject) {
13+
return JSON.parse(app.htmlUtils.htmlEncodeText(JSON.stringify(jsonObject)));
14+
},
15+
16+
htmlDecodeJson: function (jsonObject) {
17+
return JSON.parse(app.htmlUtils.htmlDecodeText(JSON.stringify(jsonObject)));
18+
}
19+
};
20+
})();

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/wwwroot/view-resources/Views/Roles/Index.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@
88
var _$rolesTable = _$table.DataTable({
99
paging: true,
1010
serverSide: true,
11-
ajax: function (data, callback, settings) {
12-
var filter = $('#RolesSearchForm').serializeFormToObject(true);
13-
filter.maxResultCount = data.length;
14-
filter.skipCount = data.start;
15-
16-
abp.ui.setBusy(_$table);
17-
_roleService.getAll(filter).done(function (result) {
18-
callback({
19-
recordsTotal: result.totalCount,
20-
recordsFiltered: result.totalCount,
21-
data: result.items
22-
});
23-
}).always(function () {
24-
abp.ui.clearBusy(_$table);
25-
});
11+
listAction: {
12+
ajaxFunction: _roleService.getAll,
13+
inputFilter: function () {
14+
return $('#RolesSearchForm').serializeFormToObject(true);
15+
}
2616
},
2717
buttons: [
2818
{
@@ -121,7 +111,8 @@
121111
success: function (content) {
122112
$('#RoleEditModal div.modal-content').html(content);
123113
},
124-
error: function (e) { }
114+
error: function (e) {
115+
}
125116
})
126117
});
127118

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/wwwroot/view-resources/Views/Tenants/Index.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@
88
var _$tenantsTable = _$table.DataTable({
99
paging: true,
1010
serverSide: true,
11-
ajax: function (data, callback, settings) {
12-
var filter = $('#TenantsSearchForm').serializeFormToObject(true);
13-
filter.maxResultCount = data.length;
14-
filter.skipCount = data.start;
15-
16-
abp.ui.setBusy(_$table);
17-
_tenantService.getAll(filter).done(function (result) {
18-
callback({
19-
recordsTotal: result.totalCount,
20-
recordsFiltered: result.totalCount,
21-
data: result.items
22-
});
23-
}).always(function () {
24-
abp.ui.clearBusy(_$table);
25-
});
11+
listAction: {
12+
ajaxFunction: _tenantService.getAll,
13+
inputFilter: function () {
14+
return $('#TenantsSearchForm').serializeFormToObject(true);
15+
}
2616
},
2717
buttons: [
2818
{
@@ -119,7 +109,8 @@
119109
success: function (content) {
120110
$('#TenantEditModal div.modal-content').html(content);
121111
},
122-
error: function (e) { }
112+
error: function (e) {
113+
}
123114
});
124115
});
125116

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/wwwroot/view-resources/Views/Users/Index.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@
88
var _$usersTable = _$table.DataTable({
99
paging: true,
1010
serverSide: true,
11-
ajax: function (data, callback, settings) {
12-
var filter = $('#UsersSearchForm').serializeFormToObject(true);
13-
filter.maxResultCount = data.length;
14-
filter.skipCount = data.start;
15-
16-
abp.ui.setBusy(_$table);
17-
_userService.getAll(filter).done(function (result) {
18-
callback({
19-
recordsTotal: result.totalCount,
20-
recordsFiltered: result.totalCount,
21-
data: result.items
22-
});
23-
}).always(function () {
24-
abp.ui.clearBusy(_$table);
25-
});
11+
listAction: {
12+
ajaxFunction: _userService.getAll,
13+
inputFilter: function () {
14+
return $('#UsersSearchForm').serializeFormToObject(true);
15+
}
2616
},
2717
buttons: [
2818
{
@@ -157,7 +147,8 @@
157147
success: function (content) {
158148
$('#UserEditModal div.modal-content').html(content);
159149
},
160-
error: function (e) { }
150+
error: function (e) {
151+
}
161152
});
162153
});
163154

0 commit comments

Comments
 (0)