2013年10月3日 星期四

Post Test

今天為公司一個新項目進行數據搜集,印證概念能否變為真實,需要到不同的網頁擷取資料。為了方便工作,寫了一個簡單的 Javascript + PHP 程式去讀取資料。
<script>
function PHLog(message)  {
 var now = new Date();
 var hours = now.getHours();
 var minutes = now.getMinutes();
 var seconds = now.getSeconds();
 var currentTime = hours+":"+minutes+":"+seconds;
 var postResult = document.querySelector("#postResult");
 postResult.value = currentTime+" "+message;
}

function postRequest()  {
 PHLog("Sending request...");
 var urlString = "http://www.pacess.com/blog_files/postTest.php";
 var parameterString = "url="+encodeURIComponent(document.querySelector("#postFunction").value)+
        "&param="+encodeURIComponent(document.querySelector("#postParameter").value);

 var http = new XMLHttpRequest();    
 http.open("post", urlString, true);
 http.setRequestHeader("content-type", "application/x-www-form-urlencoded");
 http.setRequestHeader("content-length", parameterString.length);
 http.setRequestHeader("connection", "close");
 http.onreadystatechange = function()  {
  PHLog(http.responseText+"\r\n~End~");
 };
 http.send(parameterString);
}
</script>

<input id="postFunction" type="text" placeholder="http://www.facebook.com/" style="width:300px;" value=""></input><br>
<input id="postParameter" type="text" placeholder="param=1234&time=4567" style="width:400px;" value=""></input>
<button onClick="JavaScript:postRequest();">讀取內容</button>
<br><textarea id="postResult" rows="40" style="width:580px;"></textarea>

沒有留言: