HTML5 brings many features and improvements to web form creation. There are new attributes and input types that were introduced to help create better experiences for web users.
Form creation is done in HTML5 the same way as it was in HTML4:
HTML5為Web表單創(chuàng)建帶來了許多功能和改進(jìn)。 引入了新的屬性和輸入類型,以幫助為網(wǎng)絡(luò)用戶創(chuàng)造更好的體驗。
HTML中的表單創(chuàng)建方式與HTML4中一樣:
HTML5 has introduced a new attribute called placeholder. On <input> and <textarea> elements, this attribute provides a hint to the user of what information can be entered into the field.
HTML5引入了一個名為placeholder的新屬性。 在<input>和<textarea>元素上,此屬性向用戶提供可以在該字段中輸入哪些信息的提示。
The autofocus attribute makes the desired input focus when the form loads:
The "required" attribute is used to make the input elements required.
The form will not be submitted without filling in the required fields.
The autocomplete attribute specifies whether a form or input field should have autocomplete turned on or off.When autocomplete is on, the browser automatically complete values based on values that the user has entered before.
autocomplete屬性指定表單或輸入字段是否應(yīng)該啟用或禁用自動完成。
當(dāng)自動填充開啟時,瀏覽器會根據(jù)用戶之前輸入的值自動完成值。
HTML5添加了幾種新的輸入類型:- color- date- datetime- datetime-local- email- month- number- range- search- tel- time- url- weekNew input attributes in HTML5:- autofocus- form- formaction- formenctype- formmethod- formnovalidate- formtarget- height and width- list- min and max- multiple- pattern (regexp)- placeholder- required- step
Search Options
The <datalist> tag can be used to define a list of pre-defined options for the search field:
<datalist>標(biāo)簽可用于定義搜索字段的預(yù)定義選項列表:
<option> defines the options in a drop-down list for the user to select. The ID of the datalist element must match with the list attribute of the input box.
<option>在用戶選擇的下拉列表中定義選項。
datalist元素的ID必須與輸入框的list屬性相匹配。
Some other new input types include email, url, and tel:
These are especially useful when opening a page on a modern mobile device, which recognizes the input types and opens a corresponding keyboard matching the field's type:
當(dāng)在現(xiàn)代移動設(shè)備上打開頁面時,這些功能特別有用,它識別輸入類型并打開與字段類型匹配的相應(yīng)鍵盤:
These new types make it easier to structure and validate HTML forms.
這些新類型使得HTML表單更易于構(gòu)造和驗證。
文地址:https://www.cnblogs.com/zhanglei93/p/6273655.html
作者:best.lei
數(shù)據(jù)綁定和表單標(biāo)簽
數(shù)據(jù)綁定是將用戶輸入綁定到領(lǐng)域模型的一種特性,在Spring MVC的controller和view數(shù)據(jù)傳遞中,基于HTTP請求的特性,所有HTTP請求參數(shù)的類型均為字符串,如果模型領(lǐng)域需要綁定的類型為double或int,則需要手動進(jìn)行類型轉(zhuǎn)換,而有了數(shù)據(jù)綁定后,就不需要手動將HTTP請求中的String類型轉(zhuǎn)換為模型需要的類型了,數(shù)據(jù)綁定的另一個好處是,當(dāng)輸入驗證失敗時,會重新生成一個HTML表單,無需重新填寫輸入字段。
表單標(biāo)簽庫中包含了可以用在JSP頁面中渲染HTML元素的標(biāo)簽。為了使用這些標(biāo)簽,必須在JSP頁面開頭處聲明taglib指令。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
表單標(biāo)簽庫中有input、password、hidden、textarea、checkbox、checkboxes、radiobutton、radiobuttons、select、option、options、errors。表單標(biāo)簽有acceptCharset、commandName、cssClass、cssStyle、htmlEscape、modelAttribute等屬性。
<form:form modelAttribute="user" method="post" action="userSave"> <fieldset> <p> <label for="name">用戶名:</label> <form:input path="userName"/> </p> </fieldset> </form:form>
如上分別介紹了數(shù)據(jù)綁定的定義和優(yōu)勢,以及一些表單標(biāo)簽,為了讓大家能進(jìn)一步了解上面的內(nèi)容,范例中實現(xiàn)了用戶類屬性和JSP中表單的綁定,同時在JSP中分別展示了input、password、textarea、checkbox、select標(biāo)簽。
我們首先看一下User類,類中包含User的屬性,以及對屬性字段的get和set方法:
public class User { private String userName; //用戶名 private String password; //密碼 private Integer sex; //性別 private boolean marriage; //是否結(jié)婚 private ArrayList<String> hobby; //興趣愛好 private ArrayList<String> contacts;//人脈 private String carrer; //職業(yè) private String houseRegister; //戶籍 private String remark; //個人描述 public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getHouseRegister() { return houseRegister; } public void setHouseRegister(String houseRegister) { this.houseRegister = houseRegister; } public String getCarrer() { return carrer; } public void setCarrer(String carrer) { this.carrer = carrer; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public boolean isMarriage() { return marriage; } public void setMarriage(boolean marriage) { this.marriage = marriage; } public ArrayList<String> getHobby() { return hobby; } public void setHobby(ArrayList<String> hobby) { this.hobby = hobby; } public ArrayList<String> getContacts() { return contacts; } public void setContacts(ArrayList<String> contacts) { this.contacts = contacts; } }
我們的Controller類中定義映射請求的方法,其中包括處理userInput請求的inputUser方法,以及userSave請求的addUser方法,其中在addUser方法中用到了重定向。其中通過@Autowired注釋在ProductController對象中主動注入UserService對象,實現(xiàn)對user對象的保存和查詢等操作;通過model的addAttribute方法將User類對象、HashMap類型的hobbys對象、String[]類型的carrers對象傳遞給View(JSP),代碼如下:
同時,為了避免中文亂碼的問題,需要在web.xml文件中增加編碼過濾器,同時JSP頁面編碼設(shè)置為UTF-8,form表單的提交方式必須為post,我們先看web.xml文件的配置信息:
UserAddJSP文件格式如下,其中將Map類型的hobbys綁定到了checkboxes上,將String[]類型的carrer綁定到了select上,實現(xiàn)了通過option標(biāo)簽對select添加選項,同時method方法需指定為post來避免中文亂碼的問題。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Add a User</title> </head> <body> <div id="global"> <form:form modelAttribute="user" method="post" action="userSave"> <fieldset> <legend>Add a User</legend> <p> <label>用戶名:</label> <form:input path="userName"/> </p> <p> <label>密碼:</label> <form:password path="password"/> </p> <p> <label>婚姻:</label> <form:checkbox path="marriage" value="已婚"/> </p> <p> <label>愛好:</label> <form:checkboxes items="${hobbys}" path="hobby"/> </p> <p> <label>人脈:</label> <form:checkbox path="contacts" value="張三"/>張三 <form:checkbox path="contacts" value="李四"/>李四 <form:checkbox path="contacts" value="王五"/>王五 <form:checkbox path="contacts" value="趙六"/>趙六 </p> <p> <label>職業(yè):</label> <form:select path="carrer"> <option/>請選擇職業(yè) <form:options items="${carrers }"/> </form:select> </p> <p> <label>戶籍:</label> <form:select path="houseRegister"> <option>請選擇戶籍</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">廣州</option> <option value="4">深圳</option> <option value="5">其它</option> </form:select> </p> <p> <label>個人描述:</label> <form:textarea path="remark" rows="5"/> </p> <p id="buttons"> <input id="reset" type="reset"> <input id="submit" type="submit" value="Add User"> </p> </fieldset> </form:form> </div> </body> </html>
UserList.JSP文件實現(xiàn)對保存的user信息的遍歷展示。
因為該工程是在上一篇工程中增加的,因此其他的配置文件與上一篇工程中相同,這里不再做過多的贅述,希望讀者見諒。
運行該程序在瀏覽器中輸入http://localhost:8081/SpringMVC/userInput可以看到左圖,填寫表單完成,點擊Add User按鈕,即可看到右圖的輸出信息,不知道讀者是否發(fā)現(xiàn)輸出信息中存在問題,興趣愛好為[1],戶籍也為1,輸出的為表單中選擇的索引位置,還有好像保單中忘了指定User的性別了,導(dǎo)致輸入性別列為空。這些問題是由于本文的疏忽造成的,有興趣的讀者可以對這篇博客做更加完善的改正,謝謝閱讀!
天內(nèi)容好長,翻譯起來有點累。
Preparations
With the basics of HTTP out of the way, let's get acquainted with the tools we'll use in this book to demonstrate how HTTP works. This section goes through a few tools you'll need to follow along. Note that you only need one of the tools listed. It is important to note that you will be able to follow this book regardless of what tool you use, so pick one that you're comfortable with and get started!
翻譯:
準(zhǔn)備工作,我們先學(xué)習(xí)下工具來演練 HTTP怎么工作的。本章節(jié)介紹下一些工具,可以選擇一個你覺得比較合適的來上手。
HTTP GUI Tools
We'll make heavy use of Graphical HTTP tools throughout this book. There are lots of options available in this category of HTTP tools, and we'll be using Paw 3. Although it's a paid App in the Mac App Store, there is a limited Trial version as well which can get you through the book.
That said, there are many other alternatives out there. Some other capable options are Insomnia and Postman; they're both free and they work on OS X, Windows, and Ubuntu.
HTTP 的 圖形用戶界面工具
我們這里用的是Paw3, 雖然他需要在蘋果商店買,但是也有短時間免費版本。
你也可以選擇 Insomnia 和 Postman (譯者之前裝過postman,這里就用Postman了)
HTTP Command line Tools
curl is a free command line tool that is used to issue HTTP requests.
Mac OS X/Linux :
It is shipped with OS X and most GNU/Linux distributions, and you can simply invoke it on the command line by issuing the command below.
$ curl www.google.com
If you have version 1803 or later of Windows 10, cURL should be installed by default. If you have an earlier version of windows or cannot find cURL on your version, we recommend installing a GUI version of cURL. You won't be able to execute the command line commands, but you should still be able to perform the appropriate actions in the GUI.
curl 是一個免費的工具,用來發(fā)送HTTP 請求,和MAC 及絕大多數(shù) Linux 系統(tǒng)原生綁定。
你可以很簡單的啟動curl ,用下面命令行:
$ curl www.google.com
如果你有windows10的 1803 及以后版本,curl 也是默認(rèn)安裝好了,如果你有更早的版本,你可以考慮安裝一個,圖形界面的curl. 你雖然沒法執(zhí)行命令,但是可以在GUI 中執(zhí)行相關(guān)操作。
Making HTTP Requests
HTTP Request with a Browser
Making an HTTP request is easy. Say you want to visit Reddit in your browser. All you need to do is launch your browser and enter the address https://www.reddit.com and
this is a snapshot of what you might see:
翻譯:
做HTTP的申請
通過一個瀏覽器做HTTP請求,做HTTP請求很容易,你可以在瀏覽器中訪問reddit ,直接鍵入地址https://www.reddit.com, 然后看到快照。
The server that hosts the main Reddit website handles your request and issues a response back to your browser. Your browser is smart enough to process the response that is sent back and display the site you see in the screenshot, with all its colors, images, text and presentation.
翻譯:
承載著Reddit web主站的服務(wù)器負(fù)責(zé)處理請求,并返回一個response 到瀏覽器,瀏覽器足夠“聰明”可以處理返回的響應(yīng)文件,在屏幕上把文件展示出來,包含的是顏色、圖片、文字和展示。
HTTP Request with an HTTP Tool
Because browsers show us the processed version of the response, we don't get to see the raw response the server sent back. How do we see the raw HTTP response data?
For that, we can use an HTTP tool and just like the browser did when we entered a URL in the address bar, we can have our HTTP tool issue a request to https://www.reddit.com.
Our HTTP tool, Paw, doesn't process the response and lets us see the raw response data, which looks something like this:
通過工具來做HTTP請求
因為瀏覽器顯示的是服務(wù)端回復(fù)(response)被 處理后的版本,我們沒拿到服務(wù)器發(fā)回來原始的回復(fù),我們怎么查看 服務(wù)器返回的HTTP 裸回復(fù)呢?
我們可以用HTTP工具 來實現(xiàn),我們在地址欄輸入URL, 我們的HTTP工具會發(fā)送一個請求到 https://www.reddit.com.我們的HTTP 工具,Paw,不會處理服務(wù)端的回復(fù)(response),那原始回復(fù)的response 是什么樣呢?如下圖
What a huge difference this raw response is from the display in your browser! If you've never seen raw HTTP response data before, this may be quite shocking. What you see here is, in fact, what your browser also receives, except it parses and processes that huge blob of data into a user-friendly format.
If you're learning about HTTP in order to become a web developer, you'll need to learn to read and process raw HTTP response data just by scanning it. Of course, you won't be able to convert it into a high-resolution picture in your head, but you should have a general idea of what the response is about. With enough experience, you can dig into the raw data and do some debugging and see exactly what's in the response.
這個和在瀏覽器上顯示的有很大的區(qū)別! 你的瀏覽器也會受到一樣的裸回復(fù)(response)數(shù)據(jù),瀏覽器可以對回復(fù)數(shù)據(jù)進(jìn)行解析,并以一個友好的方式向用戶呈現(xiàn)出來。
如果你在學(xué)習(xí)HTTP ,目的是做一個web 開發(fā)者,你需要學(xué)習(xí)和讀懂如何處理這些裸數(shù)據(jù)(response).當(dāng)然,你無法把response轉(zhuǎn)換為頭腦中一個高精度的照片,但是你應(yīng)該大概了解這個回復(fù)是什么含義。只要有足夠的經(jīng)驗,你就可以閱讀這些裸數(shù)據(jù),并做一些調(diào)試工作,看看回復(fù)(response)是什么。
我在Postman 里面輸入地址后,打開console,也可以看到下面返回的裸回復(fù)。
Using the Inspector
Every modern browser has a way to view HTTP requests and responses, and it's usually called the inspector. We're going to use the Chrome Inspector to demonstrate how to analyze your browser's HTTP communication.
Launch Chrome browser and open the Inspector by navigating to the Chrome Menu on the top right-hand corner of your browser. Select Tools > More Tools > Developer Tools. There are other ways of accessing the inspector, such as right-clicking and selecting the 'Inspector' option, or using a keyboard shortcut Ctrl+Shift+I (or Option+Command+I on a Mac).
Send a new request to Reddit by entering the address https://www.reddit.com into your browser.
With the inspector still open click on the Network tab:
翻譯:
使用檢查員(inspector)
每一個現(xiàn)代的瀏覽器都有辦法可以讀HTTP 請求和回復(fù)請求,這個方式就是檢查員。我們將使用google 瀏覽器Chrome 的檢查員(inspector)來分析瀏覽器的HTTP通信。
國內(nèi)訪問,果然時間要長很多。
4. The first thing you should notice is that there are a lot of entries there. Each entry is a separate request, which means just by visiting the URL,
your browser is making multiple requests, one for every resource (image, file, etc.). Click on the first request for the main page, www.reddit.com entry:
翻譯:
首先需要知道的是,這里很多的條目,每個條目是一個單獨的請求。這意味著(敲黑板),即使只是訪問url,你的瀏覽器已經(jīng)做了多個request,針對每個資源會申請一次(資源包括圖片、文件等等)。鼠標(biāo)點擊主頁條目看一下,www.reddit.com 條目:
From here, you'll be able to see the specific request headers, cookies as well as the raw response data:
翻譯:
這里可以看到針對這個主頁的請求,cookie 以及裸回復(fù)(response)。
The response data should look similar to what we saw earlier using our HTTP tool, except Chrome displays the data in a single line.
Another thing to note when using the inspector's Network tab is, other than the first request, there are a ton of other requests returned:
翻譯:
回復(fù)的數(shù)據(jù)應(yīng)該和之前我們用HTTP 工具 看到的內(nèi)容是相似的,區(qū)別是google 瀏覽器在單行中顯示數(shù)據(jù)。(這里在新版本google 瀏覽器已經(jīng)改過來了)
另外一個需要關(guān)注的是,使用inspector network 這個表,除了看到第一個請求,還有非常多的其它請求返回。(如下圖)
Why are these additional responses sent back, who initiated the requests? What's happening is that the resource we requested, the initial www.reddit.com entry, returned some HTML. And in that HTML body are references to other resources like images, css stylesheets, javascript files and more. Your browser, being smart and helpful, understands that in order to produce a visually appealing presentation, it has to go and grab all these referenced resources. Hence, the browser will make separate requests for each resource referenced in the initial response. When you scroll down the Network tab, you'll be able to see all the referenced resources. These other requests are to make sure the page displays properly on your screen, among other things. Overall, you see that the browser's inspector gives you a good feel for these referenced resources. A pure HTTP tool, on the other hand, returns one huge response chunk without any concern for automatically pulling in referenced resources. A curl request will demonstrate this:
翻譯:
為什么會有額外的回復(fù)被發(fā)回來? 第一個請求條目,www.reddit.com 返回來一些HTML 的文件。這個文件指向了其它的資源,比如圖片、CSS、JS 文件。而你的瀏覽器足夠聰明和幫助,理解了這個HTML,并去抓去了所有相關(guān)的資源。
這樣,在最開始的回復(fù)后,瀏覽器會針對每個資源做單獨的申請。當(dāng)你在network 表往下查看所有的表,你能夠看到所有被引用的資源。這些其它的請求可以幫助你把頁面在你的屏幕顯示的更加清晰,總的來看,瀏覽器inspector 給你一個好的感覺,針對所有的資源。
一個純HTTP 工具,只會返回一個巨大的response 片段,不會考慮自動的去把關(guān)聯(lián)到的資源拉群進(jìn)來。一個curl 工具一般顯示的內(nèi)容如下:
Reddit now requires that we add in a User-Agent to our HTTP requests. Otherwise, it will deny our request, assuming that the request originates from a bot.
Make sure to append the following to any curl commands where reddit is the site you wish to send a request to.
-A 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36'
The -A option is used to specify a User-Agent for an HTTP request when using curl. Since this is another option for our command,
don't forget to add in a space between -v and -A.
For the sake of simplicity, we specify the User-Agent that is listed at the end of this page. You may use your own User-Agent as well.
$ curl -X GET "https://www.reddit.com/" -m 30 -v
What you should see is just one request and the response containing the HTML, but no additional requests being automatically issued, like you see in a browser.
翻譯:
下圖,curl 百度的效果圖:
Request Methods
Let's revisit the diagram from Step 3 above, when we looked at the responses in the Network tab. You might have noticed two columns named Method and Status.
If you don't see the Methodcolumn, it may be hidden by default. To display the Method column, right click on Status and select Method.
The Method column should now be visible next to the Status column.
翻譯:
請求方法
讓我們重新調(diào)到第三步(在google瀏覽器inspector那一步),我們在network 里面查看responses的時候,你可以看到兩列:method 和 status.在status 狀態(tài)上右鍵,選擇展示method,如下圖。
We'll spend this section looking at what the information shown in these columns mean.
Information displayed in the Method column is known as the HTTP Request Method. You can think of this as the verb that tells the server what action to perform on a resource. The two most common HTTP request methods you'll see are GET and POST. When you think about retrieving information, think GET, which is the most used HTTP request method. In the above diagram, you'll notice almost all of the requests use GET to retrieve the resources needed to display the web page.
The Status column shows the response status for each request. We'll talk about responses in detail later in the book. The important thing to understand is that every request gets a response, even if the response is an error -- that's still a response. (That's not 100% technically true as some requests can time out, but we'll set those rare cases aside for now.)
我們將花一點時間介紹下這些列里包含信息的含義。
當(dāng)你想獲取信息,用GET 方法,這是最常用的HTTP 請求方法,在上面的請求中,你可以看到幾乎所有的請求都是GET .狀態(tài)列顯示 每個請求的返回狀態(tài),我們在本書稍后會介紹狀態(tài)的含義。需要了解的是,每一個請求都會有一個回復(fù)(response),即使回復(fù)還是錯誤的,這仍然是一個回復(fù)。(當(dāng)然有些超時的除外)
GET Requests
GET requests are initiated by clicking a link or via the address bar of a browser. When you type an address like https://www.reddit.com into the address bar of your browser, you're making a GETrequest. You're asking the web browser to go retrieve the resource at that address, which means we've been making GET requests throughout this book. The same goes for interacting with links on web applications. The default behavior of a link is to issue a GET request to a URL. Let's make a simple GET request to https://www.reddit.com with an HTTP tool. Make sure to select GET and enter the address:
You can view the raw HTTP response and other information sent back from the web server on the right panel.curl users can enter the following command on their terminal:
$ curl -X GET "https://www.reddit.com/" -m 30 -v
We can also send query strings using an HTTP tool. Let's look at another quick example by sending a request to search for all things Michael Jackson at https://itunes.apple.com/ with query strings. The final URL will look like this:
https://itunes.apple.com/search?term=Michael%20Jackson
before submitting a request, make sure to select GET.
Here we are simply sending an HTTP GET request to the server at https://itunes.apple.com/ with parameter term=Michael%20Jackson where %20 is a URL-encoded character for SPACE.
The curl command for this example is:
$ curl -X GET "https://itunes.apple.com/search?term=Michael%20Jackson" -m 30 -v
That's all you need to know about issuing HTTP GET requests for now. The primary concepts are:
翻譯
你需要了解的是:
POST Requests
We've seen how to retrieve or ask for information from a server with GET, but what if you need to send or submit data to the server? That's where another essential HTTP request method comes in: POST. POST is used when you want to initiate some action on the server, or send data to a server. Let's see an example with our HTTP tool:
翻譯:
如果需要提交信息到服務(wù)器,這個時候需要使用HTTP 請求的另外一個方法,POST。 當(dāng)你需要對服務(wù)器發(fā)起某些操作,或者發(fā)送數(shù)據(jù)到服務(wù)器,需要POST。
Here is the curl command:
$ curl -X POST "https://echo.epa.gov" -m 30 -v
The above screenshot shows a POST request to https://echo.epa.gov and the response from the server. Typically from within a browser, you use POST when submitting a form. POST requests allow us to send much larger and sensitive data to the server, such as images or videos. For example, say we need to send our username and password to the server for authentication. We could use a GET request and send it through query strings. The flaw with this approach is obvious: our credentials become exposed instantly in the URL; that isn't what we want. Using a POST request in a form fixes this problem. POST requests also help sidestep the query string size limitation that you have with GET requests. With POST requests, we can send significantly larger forms of information to the server.
Let's see another example of making a POST request by filling out a web form. Our sample form looks like this in the browser:
翻譯:
curl 命令行如下:
$ curl -X POST "https://echo.epa.gov" -m 30 -v
After filling out the form, you'll be redirected to a page that looks like this:
Now let's switch over to our HTTP tool and simulate what we just did in the browser. Instead of filling out a form in the browser, we will send a POST request to http://al-blackjack.herokuapp.com/new_player. This is the URL that the first form (the one where we input a name) submits to:
翻譯:
我們換到HTTP 工具上來模擬我們在瀏覽器上的操作,代替我們在瀏覽器上填入信息,我們會發(fā)送Post 請求到 http://al-blackjack.herokuapp.com/new_player
我們第一個表格(我們輸入名字的)提交到這個url:
Note: You'll want to ensure that your Content-Type header is set to application/x-www-form-urlencoded. If it isn't, then your POST request won't be interpreted by the application correctly.
If you're using Paw 3, select the Form URL-Encoded tab instead of the Text tab.
If you're using Insomnia, make sure you click "Form URL Encoded" in the Body dropdown menu. And if you're using Postman, make sure the radio button for x-www-form-urlencodedis selected under the Body tab.
翻譯:
你會希望保證你的 content-type header 為 application/x-www-form-urlencoded.如果不這樣設(shè)置,服務(wù)器將無法識別請求。
如果選擇postman,記得選擇了 x-www-form-urlencode 按鈕,在body表中。
譯者在postman中的操作截圖:
303 see other的含義:(from 譯者)
Or you can use curl:
$ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v
Notice that in the screenshot and curl command we're supplying the additional parameter of player_name=albert. It has the same effect as inputting the name into the first "What's your name?" form and submitting it.
We can verify the contents using the inspector (right click and select Inspect). You'll see that the player_name parameter we're sending as part of the POST request is embedded in the form via the nameattribute of the input element:
翻譯:
或者你可以用curl 命令實現(xiàn) :
$ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v
這里 -d "player_name=Albert" 和在瀏覽器里面填入 “what's your name?”表格效果是一樣的,可以在瀏覽器里面看到, player_name 參數(shù)是保存在表格里的。
But the mystery is, how is the data we're sending being submitted to the server since it's no
t being sent through the URL? The answer to that is the HTTP body.
The body contains the data that is being transmitted in an HTTP message and is optional. In other words, an HTTP message can be sent with an empty body.
When used, the body can contain HTML, images, audio and so on. You can think of the body as the letter enclosed in an envelope, to be posted.
The POST request generated by the HTTP tool or curl is the same as you filling out the form in the browser, submitting that form, and then being redirected to the next page.
Look carefully at the raw response in the HTTP tool screenshot. The key piece of information that redirects us to the next page is specified in the field Location:
http://al-blackjack.herokuapp.com/bet. Location and its associated data is part of what is known as an HTTP response header (yes, requests have headers too, but in this case,
it's a response header). Don't worry too much about this yet as we'll discuss headers in a later section. Your browser sees the response header and automatically issues a brand
new request to the URL specified in the Location header, thereby initiating a new, unrelated request. The "Make a bet" form you see is the response from that second request.
翻譯:
不過疑問就在,這個數(shù)據(jù)是怎么提交到服務(wù)器上的,因為他沒有通過URL 提交。
答案是:HTTP body (HTTP 身體)(敲黑板),HTTP body包含了傳遞給HTTP的數(shù)據(jù),這個是可選的。
http://al-blackjack.herokuapp.com/bet. ( request 請求有Header ,這里是response 的header信息)你的瀏覽器看到了response header, 自動發(fā)起了一個新的請求到url (location 字段里指定的),這樣就看到了第二個response 表格。
Note: If you're using some other HTTP tool, like Insomnia or Postman, you may have to uncheck "automatically follow redirects" in order to see the Location response header.
If you're fuzzy on the previous paragraph, read it again. It's critical to understand that when using a browser, the browser hides a lot of the underlying HTTP request/response cycle from you. Your browser issued the initial POST request, got a response with a Location header, then issued another request without any action from you, then displayed the response from that second request. Once again, if you were using a pure HTTP tool, you'd see the Location response header from the first POST request, but the tool would not automatically issue a second request for you. (Some HTTP tools have this ability, if you check the "automatically follow redirects" option.)
翻譯:
這里就是對上文的重復(fù)解釋,不復(fù)述了。
HTTP Headers
HTTP headers allow the client and the server to send additional information during the request/response HTTP cycle. Headers are colon-separated name-value pairs that are sent in plain text. By using the Inspector, we can see these Headers. Below, you can see both the request as well as the response headers:
The above shows the various headers being transmitted during a request/response cycle. Further, we can see that the request and response contain a different set of headers under Request Headers:
The above shows the various headers being transmitted during a request/response cycle.
Further, we can see that the request and response contain a different set of headers under Request Headers:
Request Headers
Request headers give more information about the client and the resource to be fetched. Some useful request headers are:
Don't bother memorizing any of the request headers, but just know that it's part of the request being sent to the server. We'll talk about response headers in the next chapter.
翻譯:
這里講了request header 的一些信息,request header 和 response header 信息是不一樣的。
上圖里request header 里面包含有:主機(jī)名、接受的語言、用戶端agent 信息、連接信息。
Summary
This was a brief introduction on making HTTP requests. After going through this section, you should be comfortable with:
In the next chapter, we'll continue learning about HTTP by looking at HTTP responses.
翻譯:
本章節(jié)介紹了下HTTP requests 信息,
經(jīng)過本章節(jié),你可以:
使用inspector 來 查看HTTP requests 請求
使用HTTP 工具來發(fā)起GET /POST 請求
最需要了解的組件包括:
HTTP 方法
路徑
headers 頭部
消息體(針對post 請求)
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。