2013-07-24 37 views

回答

2

就像那樣。

雖然如果你的意思是http://www.等等,那麼你確實需要記住URL的http://部分。

當心the same origin policysee ways to work around it

+0

我不知道的人,我只是跑了劇本,我設置一個.php文件,將把放在d變量中的輸入放到一個.txt文件中,並且我不會得到任何結果。有任何其他想法? – bob

+0

查看瀏覽器開發人員工具的Net標籤。準確地查看正在進行的HTTP請求以及響應是什麼。 – Quentin

+0

你必須首先定義'xmlhttp'。 'var xmlhttp = new XMLHttpRequest();' – Quentin

-1

當然:)

此功能將與wahtever數據(POST)請求你喜歡(str),無論你喜歡(phplocation)。該xmlhttp.responseText是任何服務器發回:)

<script type="text/javascript"> 
function submitForm(str, phpLocation) 
{ 
    var xmlhttp; 
    if (window.XMLHttpRequest) 
    xmlhttp=new XMLHttpRequest(); 
    else 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

    xmlhttp.onreadystatechange=function() 
    { 
    if(xmlhttp.readyState<4) 
    { 
     //i'm not ready yet. Do stuff..... 
    } 
    if(xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     //Now I'm ready. Do stuff with this: xmlhttp.responseText; 
    } 
    } 
    xmlhttp.open("POST", phpLocation, true); 
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    xmlhttp.send("str=" + encodeURIComponent(str)); 
} 
</script> 
+0

這不起作用,您爲什麼使用文章?我說得到。 – bob

+0

當然,這是行不通的。你必須實際上在你的頁面上實現一些對應於你想要的動作 - 這應該在兩個if內而不是註釋中完成。要開始您的工作,請嘗試在最後一個console.log(xmlhttp.responseText)中打開您的控制檯(通常使用F12)。至於GET,我想我會告訴你一個POST例子,因爲它們有點棘手,而且我儘管你可能想學點東西。 – Fireworm

0

如果你真的想使用GET,這裏是代碼:

xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","the_page_to_send_request_to.someFileFormat",true); 
xmlhttp.send();