如何在javascript中製作像www.example.com/example.php?d=a這樣的ajax get請求? 我已經試過:ajax通過XMLHttpRequest獲取javascript表單中的請求
xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();
有人可以提供我一個工作的例子嗎? 謝謝。
如何在javascript中製作像www.example.com/example.php?d=a這樣的ajax get請求? 我已經試過:ajax通過XMLHttpRequest獲取javascript表單中的請求
xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();
有人可以提供我一個工作的例子嗎? 謝謝。
就像那樣。
雖然如果你的意思是http://www.
等等,那麼你確實需要記住URL的http://
部分。
當然:)
此功能將與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>
如果你真的想使用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();
我不知道的人,我只是跑了劇本,我設置一個.php文件,將把放在d變量中的輸入放到一個.txt文件中,並且我不會得到任何結果。有任何其他想法? – bob
查看瀏覽器開發人員工具的Net標籤。準確地查看正在進行的HTTP請求以及響應是什麼。 – Quentin
你必須首先定義'xmlhttp'。 'var xmlhttp = new XMLHttpRequest();' – Quentin