Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

javascript - How to update HTML content without using redirect after a REST request?

I have been told issuing a redirect('/') for API requests is not RESTful. I have been doing it like so:

app.delete('/items/:id', urlencodedParser, (req, res) => {
    let index = parseInt(req.params.id);
    items.splice(index, 1);

    res.redirect('/');
});

I now know this is wrong. But then what do I need to do to update the content on the HTML web page without refreshing the page manually?

question from:https://stackoverflow.com/questions/65835547/how-to-update-html-content-without-using-redirect-after-a-rest-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Generally, the issuer (e.g. the html client) would receive the confirmation (e.g. 202 accepted) and take the decision on how to render the result.

It could be the web client decides at that point to refresh, or fetches a new list and re-renders using dynamic HTML techniques -- depending on the complexity of the web site, either is fine.

You're right though, this should not be done on the server side -- future apps may not have an HTML rendering -- e.g. system integrations or native mobile apps.


In these cases, I like to return a payload that gives the client a hint on what to do next. E.g. I might include something like:

200 OK

{
  "message": "delete id xxx completed",
  "_next": "https://my-server.com/api/items"
}

It helps developers in the future decide how to proceed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...