views:

54

answers:

3

Hi there,

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

A: 

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?

TimS
I need to send some short args by GET, and another arg (which can be very long [> 2040 chars] with POST.
Silvio Iannone
A: 

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

Joel Kennedy
I tried this way, but it's like if the receiving php page doesn't accept the POST parameters. In fact the php page doesn't recognize correctly the parameters.
Silvio Iannone
So you mean when you try to use two instances of the `XMLHttpRequest` object the destination page did not receive the post variables, but when you used just one instance it worked?
Joel Kennedy
Yes, when i use only the GET parameters it works.
Silvio Iannone
+1  A: 

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));
…
Sidnicious
Exactly - it's that simple, right?!
TimS
I know that, but i read that a GET URL cannot be longer than 2048 chars. So i decided to send the TinyMCE area content by POST.
Silvio Iannone
I don't understand the problem. Can you post an example? I've updated my question with a little bit more detail.
Sidnicious