Javascript Post Request like a Form Submit

Posted by Joseph Holsten on Stack Overflow See other posts from Stack Overflow or by Joseph Holsten
Published on 2008-09-25T15:15:43Z Indexed on 2010/03/30 21:53 UTC
Read the original article Hit count: 566

Filed under:
|
|
|
|

I'm trying to direct a browser to a different page. If I wanted a GET request, I might say

document.location.href = 'http://example.com/q=a';

But the resource I'm trying to access won't respond properly unless I use a POST request. If this were not dynamically generated, I might use the HTML

<form action="http://example.com/" method="POST">
  <input type="hidden" name="q" value="a">
</form>

Then I would just submit the form from the DOM.

But really I would like JavaScript that allows me to say

post_to_url('http://example.com/', {'q':'a'});

What's the best cross browser implementation?

Edit I'm sorry I was not clear. I need a solution that changes the location of the browser, just like submitting a form. If this is possible with XMLHTTPRequest, it is not obvious. And this should not be asynchronous, nor use XML, so AJAX is not the answer.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about http