diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 96cbdeff417..6a509ca7282 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.3.1-beta33 + 9.3.1-beta34 diff --git a/src/BootstrapBlazor/Components/Ajax/AjaxOption.cs b/src/BootstrapBlazor/Components/Ajax/AjaxOption.cs index e526fcc9739..c28e1c12e4a 100644 --- a/src/BootstrapBlazor/Components/Ajax/AjaxOption.cs +++ b/src/BootstrapBlazor/Components/Ajax/AjaxOption.cs @@ -26,4 +26,10 @@ public class AjaxOption /// [NotNull] public string? Url { get; set; } + + /// + /// 获得/设置 是否获得序列化 Json 结果 参数 默认为 true + /// + [NotNull] + public bool ToJson { get; set; } } diff --git a/src/BootstrapBlazor/wwwroot/modules/ajax.js b/src/BootstrapBlazor/wwwroot/modules/ajax.js index 366967c4b66..793d042c6b4 100644 --- a/src/BootstrapBlazor/wwwroot/modules/ajax.js +++ b/src/BootstrapBlazor/wwwroot/modules/ajax.js @@ -3,6 +3,7 @@ method: 'POST', url: null, data: null, + toJson: true, ...option } @@ -11,27 +12,24 @@ return null } - const init = { - method: option.method, - headers: { - 'Content-Type': 'application/json' - } - } - - if (option.method === 'POST' && option.data) { - init.body = JSON.stringify(option.data) - } - - let json = null; + let result = null; try { - - const response = await fetch(option.url, init) - json = await response.json() + const { toJson, url, method, data } = option; + result = await fetch(url, { + method: method, + headers: { + 'Content-Type': 'application/json' + }, + body: method === 'POST' ? JSON.stringify(data) : null + }); + if (toJson === true) { + result = await result.json() + } } catch (e) { console.info(e); } - return json + return result } export function goto(url) { diff --git a/test/UnitTest/Components/AjaxTest.cs b/test/UnitTest/Components/AjaxTest.cs index 98222ad4c14..9ba34065910 100644 --- a/test/UnitTest/Components/AjaxTest.cs +++ b/test/UnitTest/Components/AjaxTest.cs @@ -14,10 +14,12 @@ public async Task Ajax_Test() { Url = "/api/Login", Method = "POST", - Data = new { UserName = "admin", Password = "1234567" } + Data = new { UserName = "admin", Password = "1234567" }, + ToJson = false }; Assert.Equal("/api/Login", option.Url); Assert.Equal("POST", option.Method); + Assert.False(option.ToJson); Assert.NotNull(option.Data); var service = Context.Services.GetRequiredService();