簡體   English   中英

同時發送GET和POST AJAX請求

[英]Sending GET and POST AJAX requests at the same time

我只是想知道是否有可能同時發送GET和POST AJAX請求,以及如何使用XMLHttpRequest對象進行發送。

謝謝大家的幫助:D

POST發送請求。 HTTP請求只能有一種方法,但是沒有什么可以阻止您使用POST URL上的參數。

如果您POSThttp://example.com/form?foo=bar ,你仍然可以訪問foo作為GET參數。


這是使用jQuery的示例:

$.post("http://example.com/form?" + $.param({foo: "bar"}), {text: tinyMCEBody})

如果沒有jQuery,它將看起來像這樣:

…
request.open("POST","form?foo=bar",true);
request.send("text=" + encodeURIComponent(tinyMCEBody));
…

您是說要隨POST發送一些查詢字符串值嗎? 當然,只是在它們后面附加帖子網址?

您能否只創建兩個XMLHttpRequest實例? 因此,您可以:

得到

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp1=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp1.onreadystatechange=function()
  {
  if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp1.responseText;
    }
  }
xmlhttp1.open("GET","ajax_info.txt",true);
xmlhttp1.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

開機自檢

<html>
<head>
<script type="text/javascript">
function loadXMLDoc2()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp2=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp2.onreadystatechange=function()
  {
  if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp2.responseText;
    }
  }
//Set POST params
var params = "lorem=ipsum&name=binny";

xmlhttp2.open("POST","ajax_info.txt",true);
xmlhttp2.send(params);
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc2()">Change Content</button>

</body>
</html>

我更改的只是對象的名稱xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP")xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP")

取自W3Schools http://www.w3schools.com/ajax/default.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM