Cookie 允許我們在網(wǎng)絡瀏覽器中存儲用戶數(shù)據(jù),以便快速響應。例如,當用戶在任何 Web 應用程序中打開個人資料頁面時,網(wǎng)頁都會從服務器接收數(shù)據(jù)。服務器還發(fā)送包含要存儲在 Web 瀏覽器中的數(shù)據(jù)的 cookie。當用戶再次訪問個人資料頁面時,它會從 cookie 中獲取數(shù)據(jù),而不是從服務器中獲取數(shù)據(jù)以快速加載網(wǎng)頁。
要獲取數(shù)據(jù),瀏覽器首先查看 cookie,如果沒有找到 cookie 中存儲的數(shù)據(jù),則會向服務器請求。本教程將教我們?nèi)绾卧?JavaScript 中將 cookie 名稱-值對序列化為設置的 cookie 標頭字符串。
為什么我們需要序列化 ??cookie 名稱-值對?
我們可以將cookie作為鍵值對存儲在瀏覽器中,并且cookie不接受名稱值對中的一些特殊字符,如下所示。
\ " / [ ] ( ) < > ? = { } @ , ; :
登錄后復制
所以,我們需要將上面的字符替換為特殊字符的UTF-8編碼。例如,我們需要用“%20”轉(zhuǎn)義序列替換空格。
使用encodeURIComponent()方法在JavaScript中序列化cookie
encodeURIComponent() 允許開發(fā)人員通過用一個、兩個、三個或四個轉(zhuǎn)義序列替換特殊字符來對字符串進行編碼。這里,轉(zhuǎn)義序列代表字符的 UTF-8 編碼。
語法
用戶可以按照下面的語法使用encodeURIComponent()方法對URI進行編碼。
encodeURIComponent(key); encodeURIComponent(value);
登錄后復制
在上面的語法中,encodeURIComponent()方法分別獲取cookies的鍵和值,并通過用轉(zhuǎn)義序列替換特殊字符來對它們進行編碼。
示例
在下面的示例中,我們創(chuàng)建了serializeCookies()函數(shù),該函數(shù)將鍵和值作為參數(shù)。之后,我們使用encodeURIComponent()方法分別對鍵和值進行編碼。接下來,我們使用字符串文字來分隔其鍵值對‘=’字符。
在輸出中,我們可以觀察到轉(zhuǎn)義序列替換了特殊字符。
<html> <body> <h3>Using the <i> encodeURIComponent() </i> method to serialize cookies in JavaScript</h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); function serializeCookies(key, value) { let serializeKey = encodeURIComponent(key); let serializeValue = encodeURIComponent(value); let serializeCookie = serializeKey + "=" + serializeValue; return serializeCookie; } output.innerHTML += "The key is name, and the value is Shubham Vora. <br>"; output.innerHTML += "After serializing the cookies key-value pair, result is " + serializeCookies("name", "Shubham Vora"); </script> </body> </html>
登錄后復制
示例
在下面的示例中,我們創(chuàng)建了箭頭函數(shù)來序列化 cookie。我們編寫了單行函數(shù)來對鍵值對進行編碼并返回它們。此外,我們在serializeCookies()函數(shù)的鍵值參數(shù)中使用了一些更特殊的字符,用戶可以在輸出中觀察到每個特殊字符都有不同的轉(zhuǎn)義序列。
<html> <body> <h3>Using the <i> encodeURIComponent() </i> method to serialize cookies with arrow functions in JavaScript</h3> <div id = "output"> </div> <script> let output = document.getElementById('output'); const serializeCookies = (key, value) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}` output.innerHTML += "The key is key@#$12 and value is Val&^%12#$. <br>"; output.innerHTML += "After serializing the cookies key-value pair, result is " + serializeCookies("key@#$12", "Val&^%12#$"); </script> </body> </html>
登錄后復制
示例
在下面的示例中,我們創(chuàng)建了兩個輸入字段。一種是將key作為輸入,另一種是將value作為輸入。之后,當用戶單擊提交按鈕時,它會調(diào)用serializeCookies()函數(shù),該函數(shù)訪問輸入值并使用encodeURIComponent()方法對它們進行編碼。
<html> <body> <h3>Using the <i> encodeURIComponent() </i> method to serialize cookies in JavaScript</h3> <label for = "key"> Enter Key </label> <input type = "text" id = "key"> <br> <br> <label for = "Value"> Enter Value </label> <input type = "text" id = "Value"> <br> <br> <div id = "output"> </div> <br> <button onclick = "serializeCookies()"> Submit </button> <script> let output = document.getElementById('output'); function serializeCookies() { let key = document.getElementById('key').value; let value = document.getElementById('Value'); output.innerHTML = "The encoded key-value pair is " + `${encodeURIComponent(key)}=${encodeURIComponent(value)}` } </script> </body> </html>
登錄后復制
在本教程中,我們學習了使用encodeURIComponent()方法序列化cookie的鍵值對。此外,我們還看到了序列化 cookie 的不同示例。在最后一個示例中,用戶可以添加自定義輸入,并觀察 cookie 的編碼值。
以上就是如何在 JavaScript 中將 cookie 名稱-值對序列化為 Set Cookie 標頭字符串?的詳細內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!