0

javascript で www.example.com/example.php?d=a のような ajax get リクエストを作成するにはどうすればよいですか? 私はもう試した:

xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();

誰かが私に実用的な例を提供できますか? ありがとう。

4

3 に答える 3

2

そのように。

etc などを意味する場合でも、URLhttp://www.の一部を覚えておく必要があります。http://

同じ生成元ポリシーに注意し、それを回避する方法を確認してください。

于 2013-07-24T13:52:24.647 に答える
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();
于 2013-07-24T14:41:45.927 に答える
-1

もちろん :)

strこの関数は、任意のデータ ( ) を任意の場所 ( ) に送信する (POST) リクエストを作成しますphplocationxmlhttp.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>
于 2013-07-24T13:56:11.267 に答える