HTTP 204响应在iOS中返回空白页,是否可以阻止此问题? [英] A http 204 response returns a blank page in iOS, is there a way to stop this?

查看:177
本文介绍了HTTP 204响应在iOS中返回空白页,是否可以阻止此问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能以前曾被问过,但我似乎找不到解决方法,因此,如果是这种情况,我们深表歉意.

This may have been asked before but I can't seem to find a solution, so apologies if this is the case.

我在开发中有一个使用Express的简单节点应用程序.其中一条发布路线返回http 204并将其发送,以下是我的代码:

I have a simple node app in development which uses express. One of the post routes returns a http 204 and sends it, below is my code:

router.post("/:id", function(req, res, next) {
  if (!req.session.userId) {
    res.status(204).send();
  } else {
    console.log(req.params.id);
    User.findById(req.session.userId, function (err, user) {
      if (err) return handleError(err); 

      const nameForDatabase = req.params.id + "Correct";  
      console.log(nameForDatabase);

      user.set({[nameForDatabase]: req.body.correct});

      user.save(function (err, updatedUser) {
        if (err) return handleError(err);
        console.log(updatedUser);
      });
    });

    res.status(204).send();
  }
});

在桌面chrome中,此行为的行为与预期的一样,后期更新了用户数据,而页面则保持不变.在iOS中,将加载空白页.

In desktop chrome this behaves as expected, with the post updating user data, and the page otherwise remaining the same. In iOS, a blank page is loaded.

是否可以阻止空白页出现在iPhone/iPad上?

Is there a way to stop a blank page from appearing on iPhones/iPads?

非常感谢.

注意: https://simplenotesapp.herokuapp.com/fluids 是对内容的模拟我要的是,204不要加载新页面很重要,因为您会看到,一旦用户回答了问题并将其提交到底部,就会显示正确的答案.

NB: https://simplenotesapp.herokuapp.com/fluids is a mock up of what I'm going for, it's important that the 204 doesn't load a new page, as you'll see that once the user has answered questions and submitted them at the bottom, the correct answers appear.

推荐答案

在某些浏览器中,这似乎是一个众所周知的问题:

It looks like this is a somewhat known issue in some browsers:

https://forums.developer.apple.com/thread/70226

https://bugs.chromium.org/p/chromium /issues/detail?id = 368717

作为一种解决方法,您可以在网页中使用一点Javascript和Ajax来发布表单,而不是使用默认的表单发布,然后当前页面完全不会受到影响.

As a work-around, you could use a little bit of Javascript with Ajax in your web page to post your form instead of a default form post and then the current page won't be affected at all.

只需将提交"按钮挂接到Javascript上,然后执行e.preventDefault()即可阻止默认表单发布.然后,您的JS可以发布表单,而不会影响当前页面.

Just hook the submit button up to your Javascript and do e.preventDefault() to prevent the default form posting. Your JS can then post the form without affecting the current page.

这篇关于HTTP 204响应在iOS中返回空白页,是否可以阻止此问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆