jsonSubmit.html
1)能過 javascript函數驅動請求頁
<!DOCTYPE html> <html> <head> <title>把js的json 傳遞給asp文件接收並打印</title> <meta charset="utf-8"> </head> <body> <h3 onclick="queryStudent()">請求學生</h3> <script type="text/javascript"> function queryStudent() { // body... var student = { name:"張三同學", age:18, city:"江蘇宿遷" }; var myJSON = JSON.stringify(student);
//設置接收上傳json文件路徑 window.location="http://localhost:8078/test/jsonPrint.asp?student="+myJSON } </script> </body> </html>
2)jsonPrint.asp
接收JSON並解析打印出來對印子項
<!DOCTYPE html> <% dim student student=request("student") %> <html> <head> <title>json return</title> <meta charset="utf-8"> </head> <body> <% '實現讀取解析方法自定義函數 Dim scriptCtrl Function parseJSON(str) If Not IsObject(scriptCtrl) Then Set scriptCtrl = Server.CreateObject("MSScriptControl.ScriptControl") scriptCtrl.Language = "JScript" scriptCtrl.AddCode "Array.prototype.get = function(x) { return this[x]; }; var result = null;" End If scriptCtrl.ExecuteStatement "result = " & str & ";" Set parseJSON = scriptCtrl.CodeObject.result End Function %> <% set stuJSON=parseJSON(student) Response.Write("</h2>學生json原文:"+student+"</h2><br/>") response.Write("<h2>學生姓名:"+stuJSON.name+"</h2>") response.Write("<h2>學生姓名:"+stuJSON.name+"</h2>") response.Write("<h2>學生姓名:"+stuJSON.name+"</h2>") set stuJSON=nothing %> </body> </html>