0

I know there are lots of similar questions, but a tricky idea has come to my mind.

It is known that a download HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment") can stop the browser from refreshing page, because a download itself is a response and a refreshed page cannot be generated as another response. Of course I'm not going to make a download just to get rid of page refresh, but I'm thinking of if there is any other form of HTTP response that can 'replace' the page refresh, or any method of HttpContext.Current.Response that can clear the default response. Any idea?

(Don't ask me why it has to be without AJAX. I know it can work. Just not preferred at the moment)

Ursidae
  • 87
  • 9
  • I think you need to give a bit more background on what you are trying to achieve here. You want the browser to send the POST request, but you don't want to get the response..? – user1429080 May 06 '19 at 12:51
  • @user1429080 I'm doing task scheduling stuff for download. As nothing is updated due to the submission, no page refresh is intended. In addition, I have another option which is to download immediately. I would like to make two options consistent in whether refresh or not refresh. I can generate a refresh for immediate download by `setTimeout`, but it's not preferred either because the time required is unknown (it might be long). Therefore, I am now looking for a refresh prevention for scheduled download. – Ursidae May 07 '19 at 01:32

2 Answers2

0

If you don't have to send data in POST body, then you can utilize an image object and just set it's src to the server URL, this will execute the code on server and will NOT refresh our page. It's kinda AJAX but NOT in a regular way.

Following is an example JS code

function execute_test_method_on_server(param1, param2) {
    var u = "/api/somecontroller/test_get_method?param1=" + param1 + "&param2=" + param2;
    (new Image).src = u;
    return true;
}
sallushan
  • 1,134
  • 8
  • 16
0

Just discovered the iframe way.

Submit form without page reloading

Not what I expected for, but in the same direction which I have been looking for, though a 'meaningless' element needs to be created.

Ursidae
  • 87
  • 9