Mala
Mala

Reputation: 14843

JS: cross-domain POST, don't need the response

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?

Upvotes: 2

Views: 660

Answers (2)

daniel
daniel

Reputation: 1158

Not sure how well this would work...

img = new Image(); 
img.src= "http://domain.com/index.php?any_parameters_you_need_to_send_here";

Upvotes: 0

jamesmortensen
jamesmortensen

Reputation: 34048

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();

Upvotes: 3

Related Questions