第一步,在 iframe 的事件 onload 加個函式 getContentFromServiceContainer,當它載入完成後,來準備抓內容中的資料/
<iframe name="serviceContainer" src="http://localhost:2363/jsonNet20Test2/jsonServices.ashx" width="100%" onload="getContentFromServiceContainer();"></iframe>再來,利用 window.frames["serviceContainer"] 取得 iframe 物件,再用它的 .document.documentElement.outerText 拿到裡面的純文字/
function getContentFromServiceContainer() { var serviceContainer = window.frames["serviceContainer"]; var content = serviceContainer.document.documentElement.outerText; var result = document.getElementById("result"); result.innerHTML = ""; if (content == "") { result.innerHTML = "nothing happened."; return; } try { var loginResult = JSON.parse(content); result.innerHTML = "isSuccess: " + loginResult.isSuccess.toString(); if (!loginResult.isSuccess) { result.innerHTML += " errMsg: " + loginResult.errMsg; } } catch (err) { result.innerHTML = "failed parsing! " + err.toString(); } }最後,把 Server 回傳的值清乾淨,順便調慢一點,這裡給它慢一秒/
System.Threading.Thread.Sleep(1000); context.Response.ContentType = "text/plain"; string jsonLoginConditions = context.Request.Form["loginConditions"] ?? ""; if (jsonLoginConditions != "") { LoginConditions loginConditions = JsonConvert.DeserializeObject<LoginConditions>(jsonLoginConditions); LoginResult loginResult = new LoginResult(); loginResult.isSuccess = false; loginResult.errMsg = string.Format("account[{0}] is invalid!", loginConditions.account); string jsonLoginResult = JsonConvert.SerializeObject(loginResult); context.Response.Write(jsonLoginResult); }
Chrome, IE7 的結果畫面 |
修改後的檔案 連結
(馬的,今天提早下班說,又被我搞到一點多 ...)
沒有留言:
張貼留言