第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么以及何時(shí) ResponseWriter 生成原始 html?

為什么以及何時(shí) ResponseWriter 生成原始 html?

Go
慕工程0101907 2021-12-20 19:27:08
我不明白為什么代碼正確生成 view.html 和 post.html 數(shù)據(jù),但將其全部顯示為原始文本。我一直在這里遵循指南,在構(gòu)建它時(shí),我認(rèn)為從 Execute 函數(shù)生成的 html 將發(fā)送到 ResponserWriter 來處理顯示它,但我得到的錯(cuò)誤似乎表明我對(duì) Execute 的理解或者 ResponseWriter 是錯(cuò)誤的。查看.html<h4>{{.Name}}</h4><font size="3">    <div>{{printf "%s" .About}}</div></font><br><font size="2" align="right">    <div align="right">{{.PostTime}}</div></font>后.html<form action="/post/" method="POST"><div><textarea name="person" rows="1" cols="30">{{printf "%s" .Name}}</textarea></div><div><textarea name="body" rows="5" cols="100">{{printf "%s" .About}}</textarea></div><div><input type="submit" value="Submit"></div><a name="bottom"></a></form>我目前一直在讀取一個(gè)空的 dataf.txt 文件。
查看完整描述

1 回答

?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊

正如所暗示的,這是因?yàn)槟形丛O(shè)置內(nèi)容類型。引自http.ResponseWriter:


// Write writes the data to the connection as part of an HTTP reply.

// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)

// before writing the data.  If the Header does not contain a

// Content-Type line, Write adds a Content-Type set to the result of passing

// the initial 512 bytes of written data to DetectContentType.

Write([]byte) (int, error)

如果你自己沒有設(shè)置內(nèi)容類型,首先調(diào)用 toResponseWriter.Write()會(huì)調(diào)用http.DetectContentType()猜測(cè)要設(shè)置的內(nèi)容。如果您發(fā)送的內(nèi)容以 開頭"<form>",則它不會(huì)被檢測(cè)為 HTML,但"text/plain; charset=utf-8"會(huì)被設(shè)置(“指示”瀏覽器將內(nèi)容顯示為文本,而不是嘗試將其解釋為 HTML)。


"<html>"例如,如果內(nèi)容以 開頭,則內(nèi)容類型"text/html; charset=utf-8"將自動(dòng)設(shè)置,無需進(jìn)一步操作即可工作。


但是,如果您知道要發(fā)送的內(nèi)容,請(qǐng)不要依賴自動(dòng)檢測(cè),而且自己設(shè)置它比在其上運(yùn)行檢測(cè)算法要快得多,因此只需在寫入/發(fā)送任何數(shù)據(jù)之前添加此行:


w.Header().Set("Content-Type", "text/html; charset=utf-8")

并使您的post.html模板成為完整、有效的 HTML 文檔。


還有一條建議:在您的代碼中,您虔誠(chéng)地省略了檢查返回的錯(cuò)誤。不要那樣做。您至少可以在控制臺(tái)上打印它們。如果您不遺漏錯(cuò)誤,您將為自己節(jié)省大量時(shí)間。


查看完整回答
反對(duì) 回復(fù) 2021-12-20
  • 1 回答
  • 0 關(guān)注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)