萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP Response.Charset 不支援?改用 AddHeader 設定編碼

問題現象

Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'Response.Charset'

在 NetBox、某些精簡版 IIS 或 特定 ASP 執行環境 中,Response.Charset 屬性不存在。

解決方案:使用 AddHeader 直接設定 Content-Type

' 原本寫法(會報錯)
' Response.CharSet = "GB2312"

' 替代寫法:直接設定完整 Content-Type 標頭
Response.AddHeader "Content-Type", "text/html; charset=gb2312"

原理解析

  • Response.Charset 是 IIS ASP 的便利屬性,底層實際做的就是設定 Content-Type 標頭的 charset 參數。
  • Response.AddHeader 是標準 HTTP 標頭操作方法,所有 ASP 相容環境皆支援。
  • 語法:Response.AddHeader "Content-Type", "媒體類型; charset=編碼"

常用編碼對照

編碼Content-Type 範例
UTF-8(推薦)text/html; charset=utf-8
Big5(繁體中文)text/html; charset=big5
GB2312(簡體中文)text/html; charset=gb2312
JSON APIapplication/json; charset=utf-8
XMLapplication/xml; charset=utf-8

完整頁面範本

<%
Response.Buffer = True
Response.AddHeader "Content-Type", "text/html; charset=utf-8"
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>ASP 編碼設定範例</title>
</head>
<body>
  <h1>中文顯示正常:測試 12345</h1>
</body>
</html>

AJAX 回應專用範本

<%
Response.AddHeader "Content-Type", "application/json; charset=utf-8"
Response.Write "{""status"":""ok"",""msg"":""中文訊息正常""}"
%>

最佳實踐建議

  1. 統一使用 UTF-8:資料庫、網頁、資料庫連線字串、檔案儲存編碼全站一致。
  2. 在 <head> 第一行放置 <meta charset="utf-8">,瀏覽器優先參考。
  3. 資料庫連線字串加入編碼:...;CharSet=utf-8(SQL Server)或確保 Access 資料庫為 Unicode 格式。
  4. 避免使用 Response.Charset,改用 AddHeader 相容性最佳。
copyright © 萬盛學電腦網 all rights reserved