首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

jQuery POST 調用 ASP 返回整頁 HTML 含代碼?伺服器端未執行腳本

     編輯:程式設計綜合問答

前端 $.post 請求 demo_test_post.asp,預期獲取純文本響應,實際返回整個 ASP 源碼(含 <% %> 標籤)。根因:伺服器未執行 ASP 腳本,直接當靜態文件返回。

現象重現


// 前端
$.post("demo_test_post.asp", {name:"Donald", city:"Duckburg"}, function(data){
    alert(data); // 彈出整個 ASP 源碼
});

// ASP 文件 demo_test_post.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim fname, city
fname = Request.Form("name")
city = Request.Form("city")
Response.Write("Dear " & fname & ". Hope you live well in " & city & ".")
%>

核心原因

  • 用記事本建立 .asp 文件,雙擊或瀏覽器直接打開 → 當文本文件讀取
  • ASP 需在 IIS (Internet Information Services) 下運行,由 asp.dll 解析執行 <% ... %>
  • 單純檔案系統訪問(file:// 協議)不會觸發 ASP 引擎

解決步驟

1. 安裝並啟用 IIS

  • Windows 功能 → Internet Information Services → 勾選 ASP(舊版需額外勾選)
  • 確認 asp.dll 註冊:regsvr32 %windir%\system32\inetsrv\asp.dll

2. 建立網站/虛擬目錄

  1. IIS 管理器 → 網站 → 右鍵「添加網站」
  2. 物理路徑指向包含 demo_test_post.asp 的資料夾
  3. 綁定埠(如 8080),主機名留空

3. 瀏覽器訪問驗證


https://localhost:8080/demo_test_post.asp?name=Donald&city=Duckburg
// 應顯示:Dear Donald. Hope you live well in Duckburg.

4. 前端調用正確 URL


$.post("https://localhost:8080/demo_test_post.asp", 
       {name:"Donald", city:"Duckburg"}, 
       function(data){ alert(data); });

替代方案:無 IIS 環境怎麼辦?

  • 換用 ASP.NET (.aspx):Visual Studio 內建 IIS Express,F5 即可調試
  • 換用 Node.js/Python/PHP:輕量級伺服器,開發體驗更好
  • 經典 ASP 已停止主流維護,新專案不建議採用

調試技巧

  • 瀏覽器網路面板查看 Response Headers:Content-Type: text/html; charset=gb2312 表示 ASP 執行了;text/plain 或直接看到源碼表示未執行
  • IIS 日誌:%windir%\System32\LogFiles\W3SVC1\
  • ASP 錯誤:在 IIS 中啟用「發送錯誤到瀏覽器」查看詳細錯誤行號

總結

$.post 返回 ASP 源碼 = 伺服器未解析執行 ASP。必須在 IIS 下以 https:// 協議訪問,而非文件系統路徑。新手建議直接學習 ASP.NET Core 或現代後端框架,避開經典 ASP 環境配置坑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved