Re: [ajax] (將code寫活的)要如何利用 javascript …

看板Ajax作者 (absszero)時間14年前 (2009/11/09 16:46), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
我將 xhr 提出來成為一個全域變數,因為showMsg()也會用到 passwd 跟 name 的部份都暫時用數值帶入。 底下是HTML的部份,不包含JSP Code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "" rel="nofollow">http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type = "text/javascript"> var xhr; function sendRequest() { var url, qs; var name, passwd; xhr = createXMLHttp(); if (xhr) { passwd = 'aaa'; name = "ee";//指派ee給name url = "test.jsp"; qs = "account=" + name + "&passwd=" + passwd; xhr.onreadystatechange = showMsg; xhr.open("POST", url, true); // 非同步傳輸 + POST xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.send(qs); //查詢字串 } } function createXMLHttp() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e1) { try { return new XMLHttpRequest(); } catch (e2) { return null; } } } } function showMsg() { var strMsg; if (xhr.readyState == 4 && xhr.status == 200) { strMsg = "<hr>已成功接收,伺服器的回傳結果是:<p>" + xhr.responseText; document.getElementById("disp").innerHTML = strMsg; } } </script> </head> <body> <form id="aForm" action="test.jsp"> 帳號:<input name="account" size="20"><br> 密碼:<input name="passwd" type="password" size="10"><p> <input type="button" value="送出去" onclick="sendRequest()"> <input type="reset" value="這次不算"></p> <div id="disp"></div> </form> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.244.36.131

11/10 16:44, , 1F
感謝你熱心回復~問題已經解決了
11/10 16:44, 1F
文章代碼(AID): #1AzzR-58 (Ajax)
文章代碼(AID): #1AzzR-58 (Ajax)