2

Without using any jQuery or other libraries, I'm trying to send a simple POST request across domains.

There are a few solutions, How Do I send a Cross Domain POST Request Via JavaScript for example, for retrieving the response and parsing it. However I'm looking for something simpler and lighter-weight given I don't need the response.

What would be the easiest way to do this?

Community
  • 1
  • 1
Mala
  • 14,178
  • 25
  • 88
  • 119
  • 1
    possible duplicate of [How do I send a cross-domain POST request via JavaScript?](http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript) I think the answer with the highest score on that question is the best way of doing this – Matti Virkkunen Feb 05 '11 at 02:57
  • Does it need to be completely clientside? What I am getting at is that you could create a proxy that your script posts to that routes your request to the remote server. – Adrian Feb 05 '11 at 03:00
  • Matti - that is the question i referenced in my question - his question includes trying to get the response. I'm wondering if there is anything simpler. – Mala Feb 05 '11 at 03:02
  • The top answer from that question doesn't use the response, just submits the form data cross-domain. – Kevin Feb 05 '11 at 03:05
  • 2
    @Mala: If you had actually *read* the link I kindly pointed you to, you would've noticed that the top answer doesn't discuss the response at all. – Matti Virkkunen Feb 05 '11 at 03:06
  • You're right - sorry about that :-\ voted to close – Mala Feb 05 '11 at 03:48

2 Answers2

3

This is perhaps as simple as it can get. Use a form element and invoke the submit functionality programmatically:

    var form1 = document.createElement("form");
    form1.setAttribute("action","http://google.com");
    form1.setAttribute("method","post");
    var input = document.createElement("input");
    input.setAttribute("name","key1");
    input.setAttribute("value","value1");
    input.setAttribute("type","text");

    form1.appendChild(input);
    var inputbtn = document.createElement("input");
    inputbtn.setAttribute("type","submit");

    form1.appendChild(inputbtn);
    document.body.appendChild(form1);
    form1.submit();
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • Will this not send the browser window away to the form result? I want the browser to stay where it is. – Mala Feb 05 '11 at 03:03
  • 1
    this will that is why in the linked solution in the comments above they constructed a form in an iframe to do the post – Adrian Feb 05 '11 at 03:05
  • @Mala - Aliester's method will work because the iframe will be redirected instead of the main page. And as long as the iframe is on your domain, you can programmatically access it using iframe.contentWindow.document. – jamesmortensen Feb 05 '11 at 03:10
  • 1
    Instead of building the form within the iframe, you could also use `form1.setAttribute('target', 'the_iframe')` to submit the form into an invisible iframe. `onload` property in iframe can help you decide if the form is submitted. – timdream Feb 05 '11 at 03:54
  • @timdream - I'll have to try that out. That seems even simpler. Thanks! – jamesmortensen Feb 05 '11 at 04:13
0

Not sure how well this would work...

img = new Image(); 
img.src= "http://domain.com/index.php?any_parameters_you_need_to_send_here";
daniel
  • 1,148
  • 1
  • 13
  • 20