简体   繁体   中英

Post request without reloading page and support for no javascript (Node Express framework)

What I would like to achieve is a regular http post to a backend endpoint. But I need a solution that will work with JS enabled and without it enabled.

Because it needs to work without JS the markup needs to be standard HTML

<form method="post" action="http://backend.service">
    ....
    <input type="submit">Submit</input>
</form>

This will cause the page to reload - which is expected.

Now I would like a JS solution, so that if JS is enabled it will not reload the page and instead do some fancy DOM manipulation to update the page.

Can anybody suggest a nice and tidy solution to this problem? I would like to avoid checks for JS enabled or not. I'm think it should be contained with a single JS file loaded on the page. If JS is enabled it will execute and maybe intercept the post request and do some sort of AJAX request instead. I'm hoping somebody has solved this problem before and has simple code and I use.

I am running within a node express server and presently have no client-side JS libraries and would like to avoid adding any if I can help it.

Thanks in advance.

I think that you have two solutions:

1) With JS enabled, use $.post to make an async call;

2) With JS disabled, use an hidden frame or iframe;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM