简体   繁体   English

发布请求而无需重新加载页面,并且不支持JavaScript(Node Express框架)

[英]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. 我想要实现的是将常规http发布到后端端点。 But I need a solution that will work with JS enabled and without it enabled. 但是我需要一个可以在启用JS和不启用JS的情况下工作的解决方案。

Because it needs to work without JS the markup needs to be standard HTML 因为它需要在没有JS的情况下工作,所以标记必须是标准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. 现在,我想要一个JS解决方案,以便如果启用JS,它将不会重新加载页面,而是执行一些高级DOM操作来更新页面。

Can anybody suggest a nice and tidy solution to this problem? 有人可以为这个问题提出一个很好而整洁的解决方案吗? I would like to avoid checks for JS enabled or not. 我想避免检查是否启用了JS。 I'm think it should be contained with a single JS file loaded on the page. 我认为它应该包含在页面上加载的单个JS文件中。 If JS is enabled it will execute and maybe intercept the post request and do some sort of AJAX request instead. 如果启用了JS,它将执行并可能拦截发布请求,然后执行某种AJAX请求。 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. 我正在节点快递服务器中运行,并且目前没有客户端JS库,如果可以的话,我希望避免添加任何内容。

Thanks in advance. 提前致谢。

I think that you have two solutions: 我认为您有两种解决方案:

1) With JS enabled, use $.post to make an async call; 1)启用JS后,使用$ .post进行异步调用;

2) With JS disabled, use an hidden frame or iframe; 2)在禁用JS的情况下,请使用隐藏框架或iframe;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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