Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
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; }
}
31 changes: 15 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,25 @@
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 = opitons.toJson;
delete options.toJson;
result = await fetch(option.url, {
method: option.method,
headers: {
'Content-Type': 'application/json'
},
body: option.method === 'POST' ? JSON.stringify(option.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