2012-11-22 40 views
0

我正在嘗試使用以下JavaScript登錄該網站。但是,我只加載完整頁面。我正在開發Windows 8應用用我的javascript登錄該站點

(function() { 
"use strict"; 

WinJS.UI.Pages.define("/pages/home/home.html", { 
    // This function is called whenever a user navigates to this page. It 
    // populates the page elements with the app's data. 
    ready: function (element, options) { 
     // TODO: Initialize the page here. 
     document.getElementById("bt_login").addEventListener("click", login, false); 
    } 
    }); 
    })(); 

function login() { 
var xhrDiv = document.getElementById("xhrReport"); 
var xhr = new XMLHttpRequest(); 


xhr.onreadystatechange = dataLoaded; 

xhr.open("POST", "http://www.160by2.com", true, <username>, <password>); 
xhr.send(); 

function dataLoaded() { 
    if (xhr.readyState === 4) { 
     if (xhr.status === 200) { 
      // OK 
      xhrDiv.innerHTML = window.toStaticHTML("response:" + xhr.responseText); 

     } else { 
      // not OK 
      xhrDiv.innerText = "failure"; 
     } 
    } 
};} 

我想在xhrdiv.innerHTML爲 「登錄成功」 或 「登錄錯誤」 來dsiplay

編輯:

我嘗試下面的代碼:

iframe = document.getElementById("ifra_op"); iframe.setAttribute("src", "http://www.160by2.com/index"); document.getElementById("op").appendChild(iframe); iframe.contentWindow.document.getElementById("MobileNoLogin") = "<mobno>"; iframe.contentWindow.document.getElementById("LoginPassword") = "<pass>; iframe.contentWindow.document.getElementsByName("LoginForm").click();

但是,出現錯誤。它說:「Javascript運行時錯誤:數學是未定義的」

「數學」來自網站。我不知道如何處理這個問題。此外,該權限被拒絕。爲什麼?

+0

究竟什麼是行不通的? – Bergi

回答

0

您需要確保在遠程服務器中有一個服務(類似Web服務)來處理請求。

在這裏你可以做的是。

  1. 創建iframe並將src設置爲160by2;
  2. 使用iframe.contentwindow訪問網頁,注入代碼填寫表單並觸發提交按鈕。
  3. 解析收到的html以驗證您的登錄。

使用jQuery,使生活更輕鬆。

var iframe = $("#ifra_op"); 
$(iframe).attr("src", "http://www.160by2.com/index"); 
$(iframe).attr("onload","submitForm();"); 
function submitForm(){ 
$(iframe.contentWindow.document).find("#MobileNoLogin").val("9999"); 
$(iframe.contentWindow.document).find("#LoginPassword").val("pass"); 
$('#button').click(); 
} 
+0

我按照你的說法試過。但是,看到我在編輯 – dinesh2707

+0

這個問題非常感謝:) – dinesh2707

+0

嘿。它說訪問被拒絕;( – dinesh2707