按下按鈕後,我將整個HTML內容從網頁(<html>
標記中的部分)發送到CGI腳本,該腳本處理內容並將其發送回去。替換整個HTML內容在HTTP請求後殺死HEAD和BODY標記!
現在我試圖用新的替換現有的內容。不幸的是,在分配後,每一個<head>
或<body>
標籤(以及關閉標籤)都將被殺死。
通過使用一些警報,我查看了返回值以及原始的HTML內容。兩者絕對如預期。
但分配後有一些魔法正在進行。請幫我弄清楚發生了什麼事。
下面是使用JavaScript代碼我使用:
var originalBodyInnerHTML = document.body.innerHTML;
var htmlNode = document.getElementsByTagName('html')[0];
var post_parameters = encodeURIComponent(htmlNode.innerHTML);
makePOSTRequest("POST", "http://whatever.com/cgi-bin/doit.cgi", post_parameters, htmlNode);
function makePOSTRequest(method, url, parameters, htmlNode) {
var http_request = getRequestObj();
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function()
{
if (http_request.readyState < 4)
{
var waitingPageBody = '< img src="/img/ajaxloader.gif" alt="in progress..."/>';
document.body.innerHTML = waitingPageBody;
}
else //if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
alert('1response: ' + http_request.responseText);
alert('2innerhtml: ' + document.getElementsByTagName('html')[0].innerHTML);
document.getElementsByTagName('html')[0].innerHTML = http_request.responseText;
}//end of if (http_request.status == 200)
else
{//other http statuses
alert("There was a problem (" + http_request.statusText + ", " + http_request.status + ' error)');
bodyNode.innerHTML = originalBodyInnerHTML;
}
}//end of else if http_request.readyState == 4
}
http_request.open(method, url, true); //async
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Accept", "application/atom+xml,application/xml,text/xml");
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function getRequestObj() {
var http_request = false;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/html');
}
}
else if (window.ActiveXObject)
{ // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
return http_request;
}
+1對於jQuery的建議 – nexneo 2009-10-23 11:15:52
是的,即時取代一切。但服務器的響應中已包含這些標籤。所以更換將正是我需要的。不幸的是,這次asignment殺死我的身體和頭標籤( 當然,我也喜歡jquery或原型,但不能在我的特殊環境中使用它... – dforce 2009-10-23 11:32:33