I work on razor asp.net .i can't make jquery ajax request to call get function
so please how to make ajax request return value on LoginStatusMsg have value then set visability true
so i need to make jquery ajax request to function ChangeLoginStatus and if LoginStatusMsg have value then set visability to true
C#
public JsonResult OnGetChangeLoginStatus(string UserId)
{
string MyUserData = Request.Cookies["myUserData"];
userAuthResponse = System.Text.Json.JsonSerializer.Deserialize(MyUserData);
UserId = userAuthResponse.Data.UserID;
string strHost = _ISecurityService.GetPCName();
DataSet ds = _IUserSecurity.ChangeLoginStatus(UserId, strNewStatus, UserId, strHost);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0][0].ToString() != "0")
{
LoginStatusMsg = ds.Tables[0].Rows[0][1].ToString();
}
}
return new JsonResult(LoginStatusMsg);
}
Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 11, 2023, 9:47 PM EDT
Please find the same code and see whether this would help.
@page @using RazorPageWithAjaxCall.Pages; @model IndexModel @{ ViewData["Title"] = "Home Page"; } AJAX CALL FROM RAZOR PAGE Get Data @section Scripts { $(document).ready(function () { $("#getDataBtn").click(function () { $.ajax({ url: "/Index?handler=GetData", type: "GET", success: function (data) { $("#result").html(data); }, error: function () { alert("Error occurred while retrieving data."); } }); }); }); }using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace RazorPageWithAjaxCall.Pages { public class IndexModel : PageModel { private readonly ILogger _logger; public IndexModel(ILogger logger) { _logger = logger; } public void OnGet() { } public IActionResult OnGetGetData() { string data = "Data retrieved from the service."; return Content(data); } } }Sample code atttached for your perusal
Amit MohantyRank #112 · 20K reputationPosted Jun 12, 2023, 5:48 AM EDT
Check this
function changeLoginStatus() { var userId = 'your_user_id'; // Replace with the actual user ID $.ajax({ url: '/YourControllerName/OnGetChangeLoginStatus', type: 'GET', data: { UserId: userId }, success: function (response) { if (response) { var loginStatusMsg = response; if (loginStatusMsg) { // Set visibility to true $('#yourElementId').show(); } else { // Set visibility to false $('#yourElementId').hide(); } } }, error: function (xhr, status, error) { // Handle the error if the AJAX request fails console.log(error); } }); }