A http 204 response returns a blank page in iOS, is there a way to stop this?

余生颓废 提交于 2020-01-25 09:40:08

问题


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:


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.



来源:https://stackoverflow.com/questions/51351482/a-http-204-response-returns-a-blank-page-in-ios-is-there-a-way-to-stop-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!