Skip to content

Instantly share code, notes, and snippets.

@batizhevsky
Created September 27, 2012 14:28
Show Gist options
  • Save batizhevsky/3794297 to your computer and use it in GitHub Desktop.
Save batizhevsky/3794297 to your computer and use it in GitHub Desktop.
cross domain post with ajax
function crossDomainPost() {
// Add the iframe with a unique name
var iframe = document.createElement("iframe");
var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.contentWindow.name = uniqueString;
// construct a form with hidden inputs, targeting the iframe
var form = document.createElement("form");
form.target = uniqueString;
form.action = "http://INSERT_YOUR_URL_HERE";
form.method = "POST";
// repeat for each parameter
var input = document.createElement("input");
input.type = "hidden";
input.name = "INSERT_YOUR_PARAMETER_NAME_HERE";
input.value = "INSERT_YOUR_PARAMETER_VALUE_HERE";
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment