siannone
siannone

Reputation: 6763

Sending GET and POST AJAX requests at the same time

i just would like to know if it is possible sending GET and POST AJAX requests at the same time and if it is how to do it using the XMLHttpRequest object.

Thanks all for helping :D

Upvotes: 1

Views: 6147

Answers (3)

s4y
s4y

Reputation: 51695

Send the request as a POST. An HTTP request can only have one method, but nothing is stopping you from using parameters on a POST URL.

If you POST to http://example.com/form?foo=bar, you'll still be able to access foo as a GET parameter.


Here's an example using jQuery:

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

Without jQuery, that would look more like this:

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

Upvotes: 6

Joel Kennedy
Joel Kennedy

Reputation: 1611

Could you just make two instances of the XMLHttpRequest? So you could have:

Get

<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>

POST

<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>

All I changed was the name of the object xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP") and xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP")

Taken from W3Schools http://www.w3schools.com/ajax/default.asp

Upvotes: 0

TimS
TimS

Reputation: 6052

Do you mean you'd like to send some query string values along with your POST? Surely it's just a case of appending them the post URL?

Upvotes: 0

Related Questions