Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.3.1-beta33</Version>
<Version>9.3.1-beta34</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/BootstrapBlazor/Components/Ajax/AjaxOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public class AjaxOption
/// </summary>
[NotNull]
public string? Url { get; set; }

/// <summary>
/// 获得/设置 是否获得序列化 Json 结果 参数 默认为 true
/// </summary>
[NotNull]
public bool ToJson { get; set; }
}
30 changes: 14 additions & 16 deletions src/BootstrapBlazor/wwwroot/modules/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
method: 'POST',
url: null,
data: null,
toJson: true,
...option
}

Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion test/UnitTest/Components/AjaxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AjaxService>();
Expand Down