
Ajax call
The ajax method used to use js directly in the browser to interact with the server currently only supports both input and output as json, and the return value is a json string, which can be converted and processed by itself.
Impersonate a login
Demo
Special attention:
HttpContext.SignInAsync
, the real use needs to refresh the page after the login is completed, otherwise the real login can not be successful.var option = new AjaxOption
{
Url = "/api/Login",
Data = new User() { UserName = "admin", Password = "1234567" }
};
var result = await AjaxService.InvokeAsync(option);
Implementing page jumps with Js
addresses an issue where Blazor
pages don't really refresh when they're jumped as SPA
Demo
Methods
@page "/ajaxs"
@inject IStringLocalizer<Ajaxs> Localizer
<h3>Ajax call</h3>
<h4>The ajax method used to use js directly in the browser to interact with the server currently only supports both input and output as json, and the return value is a json string, which can be converted and processed by itself.</h4>
<DemoBlock Introduction="Impersonate a login" Title="Basic usage" Name="Normal" Demo="typeof(Demos.Ajax.AjaxNormal)">
<p>
<b>Special attention:</b>
<div>Here is only a login simulation, and there is no real call to<code> HttpContext.SignInAsync </code>, the real use needs to refresh the page after the login is completed, otherwise the real login can not be successful.</div>
</p>
<Pre>var option = new AjaxOption
{
Url = "/api/Login",
Data = new User() { UserName = "admin", Password = "1234567" }
};
var result = await AjaxService.InvokeAsync(option);</Pre>
</DemoBlock>
<DemoBlock Introduction="Implementing page jumps with<code> Js </code> addresses an issue where <code> Blazor </code> pages don't really refresh when they're jumped as <b> SPA </b>" Title="Page jump" Name="Goto" Demo="typeof(Demos.Ajax.AjaxGoto)" />
<MethodTable Items="@GetMethods()" />
@code
{
/// <summary>
/// 获得属性方法
/// </summary>
/// <returns></returns>
private IEnumerable<MethodItem> GetMethods() => new MethodItem[]
{
new MethodItem()
{
Name = "InvokeAsync",
Description = Localizer["InvokeAsync"],
Parameters = "AjaxOption",
ReturnValue = "string"
},
new MethodItem()
{
Name = "Goto",
Description = Localizer["Goto"],
Parameters = "string",
ReturnValue = " — "
}
};
}
Error: Response status code does not indicate success: 404 (Not Found).