A http 204 response returns a blank page in iOS, i

2019-08-19 10:00发布

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

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();
  }
});

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.

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

Many thanks in advance.

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.

1条回答
Viruses.
2楼-- · 2019-08-19 10:23

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

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.

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.

查看更多
登录 后发表回答