Skip to content

Commit 038182b

Browse files
committed
aspnetboilerplate#860 Should handle error status codes for abp.ajax.
1 parent 0d0ee84 commit 038182b

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/Abp.Web.Common/Web/Api/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public string CreateScript(ApplicationApiDescriptionModel model)
1818

1919
script.AppendLine("/* This file is automatically generated by ABP framework to use MVC Controllers from javascript. */");
2020
script.AppendLine();
21-
script.AppendLine("var abp = abp | {};");
22-
script.AppendLine("abp.services = abp.services | {};");
21+
script.AppendLine("var abp = abp || {};");
22+
script.AppendLine("abp.services = abp.services || {};");
2323

2424
foreach (var module in model.Modules.Values)
2525
{
@@ -35,7 +35,7 @@ private void AddModuleScript(StringBuilder script, ModuleApiDescriptionModel mod
3535
script.AppendLine($"// module '{module.Name.ToCamelCase()}'");
3636
script.AppendLine("(function(){");
3737
script.AppendLine();
38-
script.AppendLine($" abp.services.{module.Name.ToCamelCase()} = abp.services.{module.Name.ToCamelCase()} | {{}};");
38+
script.AppendLine($" abp.services.{module.Name.ToCamelCase()} = abp.services.{module.Name.ToCamelCase()} || {{}};");
3939

4040
foreach (var controller in module.Controllers.Values)
4141
{
@@ -53,7 +53,7 @@ private void AddControllerScript(StringBuilder script, ModuleApiDescriptionModel
5353
script.AppendLine(" (function(){");
5454
script.AppendLine();
5555

56-
script.AppendLine($" abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} = abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} | {{}};");
56+
script.AppendLine($" abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} = abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} || {{}};");
5757

5858
foreach (var action in controller.Actions.Values)
5959
{

src/Abp.Web.Common/Web/Api/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Abp.Web.Api.ProxyScripting.Generators
99
{
1010
internal static class ProxyScriptingJsFuncHelper
1111
{
12-
private const string ValidJsVariableNameChars = "abcdefghijklmnopqrstuxwvyxABCDEFGHIJKLMNORQRSTUXWVYZ0123456789_";
12+
private const string ValidJsVariableNameChars = "abcdefghijklmnopqrstuxwvyxABCDEFGHIJKLMNOPQRSTUXWVYZ0123456789_";
1313

1414
public static string NormalizeJsVariableName(string name, string additionalChars = "")
1515
{

src/Abp.Web.Common/Web/Models/AjaxResponseOfTResult.cs

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class AjaxResponse<TResult>
3030
/// </summary>
3131
public bool UnAuthorizedRequest { get; set; }
3232

33+
/// <summary>
34+
/// A special signature for AJAX responses. It's used in the client to detect if this is a response wrapped by ABP.
35+
/// </summary>
36+
public bool __abp { get; } = true;
37+
3338
/// <summary>
3439
/// Creates an <see cref="AjaxResponse"/> object with <see cref="Result"/> specified.
3540
/// <see cref="Success"/> is set as true.

src/Abp.Web.Resources/Abp/Framework/scripts/libs/abp.jquery.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@
1919
return $.Deferred(function ($dfd) {
2020
$.ajax(options)
2121
.done(function (data) {
22-
abp.ajax.handleResponse(data, userOptions, $dfd);
23-
}).fail(function () {
24-
$dfd.reject.apply(this, arguments);
22+
if (data.__abp) {
23+
abp.ajax.handleResponse(data, userOptions, $dfd);
24+
} else {
25+
$dfd.resolve(data);
26+
userOptions.success && userOptions.success(data);
27+
}
28+
}).fail(function (err) {
29+
if (err.responseJSON && err.responseJSON.__abp) {
30+
abp.ajax.handleResponse(err.responseJSON, userOptions, $dfd);
31+
} else {
32+
$dfd.reject.apply(this, arguments);
33+
userOptions.error && userOptions.error.apply(this, arguments);
34+
}
2535
});
2636
});
2737
};

0 commit comments

Comments
 (0)