如何在javascript中制作ajax get请求,例如www.example.com/example.php?d=a? 我试过了:
xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();
有人能为我提供一个有效的例子吗? 感谢。
答案 0 :(得分:2)
虽然您的意思是http://www.
等等,但您确实需要记住网址的http://
部分。
答案 1 :(得分: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();
答案 2 :(得分:-1)
当然:)
此函数会将您喜欢的wahtever数据(str
)发送到(POST)请求,无论您喜欢哪个地方(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>