Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
表单处理
|
83
最后,把这些处理函数链进应用里(版本库的
ch08/meadowlark.js
):
app.get('/newsletter-signup', handlers.newsletterSignup)
app.post('/newsletter-signup/process', handlers.newsletterSignupProcess)
app.get('/newsletter-signup/thank-you', handlers.newsletterSignupThankYou)
这些就是所有需要做的了。注意在处理函数里,我们重定向到了一个“感谢订阅”页,在里
面可以渲染一个视图。但要是那样做的话,访问者浏览器中的
URL
字段还是保持
/process
,
这会令人困惑。在这种情况下,让浏览器发出一个重定向请求就可以解决问题。
在这种情形中,你要使用
303
(或
302
)重定向而不是
301
重定向,这很重
要。
301
重定向是
“永久重定向”,意味着浏览器可能会缓存重定向的目标。
如果你使用了
301
重定向并且尝试再次提交这个表单
,浏览器就可以完全绕
过
/proce
ss
这个表单处理,直接进入
/thank-you
,因为它正确地理解了这个
重定向是永久的。另外,
303
重定向就相当于告诉浏览器
,“好的,你的请求
没问题,你可以从这里获取响应”,并且不要缓存重定向的目标。
对于大多数前端框架来说,更常见的是使用
fetch
API
(一会儿就会看到),把表单数据以
JSON
格式发送出去
。不过,理解浏览器默认如何处理表单提交仍然有好处 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099