0

I'm facing an issue with ajax request in razor page. My ajax post request is mentioned below and it's hitting the OnPostabc method in my page modal.

                        type: "POST",
                        url: path + '?handler=abc',
                        contentType: 'application/x-www-form-urlencoded',
                        data: { a: b, c: d,
                        headers:
                        {
                            "RequestVerificationToken": $('input:hidden[name="__RequestVerificationToken"]').val()
                        },
                       success: function(data, textStatus) {
                        console.log("DOne");
                       }
                        
                    });

So in my OnPostabc method, i'm redirecting page using return RedirectToPage(); and i will hitting the onget method in my page modal.

 public virtual async Task<ActionResult> OnPostSaveLoanSetupAsync([FromRoute] Guid p, [FromRoute] Guid q, string a, string c)
        {
     return RedirectToPage();
    }

So the thing is, in my onget return page(),

public override async Task<ActionResult> OnGetAsync([FromRoute] Guid p, [FromRoute] Guid q)
        {
return Page();}

issue is. page is not refreshing after ajax request and not updating the url with handler

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
Harendrra
  • 107
  • 1
  • 10
  • 1
    Possible duplicate of https://stackoverflow.com/questions/10178007/mvc3-redirect-to-action-after-ajax-call – cytek04 Jun 17 '23 at 23:48
  • Use JavaScript to redirect after the ajax post is complete. – cytek04 Jun 17 '23 at 23:49
  • Hi @cytek04, I checked your mentioned link, and it's worked for me. But why we need js redirection? Why it's not refreshing from onget? – Harendrra Jun 18 '23 at 03:59
  • 2
    @Harendrra The AJAX request is made asynchronously and if you check the network tab in your browser dev tools, you will see that in your example, it returns the entire HTML for the page. The browser doesn't know what to do with that response. You need to tell it. You can either tell it to replace the current content with the response, or ignore the response and get the updated page by redirecting. – Mike Brind Jun 18 '23 at 06:35

1 Answers1

0

You can not redirect in ajax directly, As Mike Brind explained, Ajax will return the target page's html code to the front side instead of redirecting to the target directly like mvc and razor page. You need to redirect in success method. If you just redirect to target page without any parameters, you can just return RedirectToPage("xxx"); in backend and use

success: function(response){
                    // check whether response is received
                    if (response) {
                       
                        document.write(response)
                    }
                }

in your frontside.

But if you wanna redirect with some parameter, you can return Content("/Index?property1='AAA'&property2='BBB'"); in backend and use

success: function(response){
                    // check whether response is received
                    if (response) {                        
                        window.location.href = response
                        
                    }
                }

in your frontside.

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12