Take a look at the Jquery post function: http://api.jquery.com/jQuery.post/
Also, if you want to send some data, along with the url, you can do it with your get as well:
www.someurl.com?dataAttr=someValue&dataOtherAttr=someOtherDataValue
This would be the GET equivalent of a post with the following data:
{
"dataAttr": "someValue",
"dataOtherAttr": "someOtherDataValue"
}
Here is a pure js that will make a post to a server. The script makes an invisible iframe and makes a call to a specified server with the specified parameters. The server has to support the Cross Domain Policy. If you can't make the server support CORS, you are out of luck:
/**
*
* Makes a post to the specified url with the specified param - keyval
*/
makePost = function makePost(url, params){
var iframeId = 'iframeid';
var addParamsToForm = function (form, params){
var addParamToForm = function(form, paramName, paramValue){
var input = document.createElement('input');
input.hidden = 'hidden';
input.name = paramName;
input.value = paramValue;
form.appendChild(input);
}
for ( var prop in params ){
if ( params.hasOwnProperty(prop) ){
if ( params[prop] instanceof Array ){
for ( var i = 0; i < params[prop].length; i ++ ){
addParamToForm(form, prop, params[prop][i]);
}
} else {
addParamToForm(form, prop, params[prop]);
}
}
}
};
var iframe = document.getElementById(iframeId);
if ( iframe === null ){
iframe = document.createElement('iframe');
iframe.name = 'iframeName';
iframe.id = iframeId;
iframe.setAttribute("style", "width: 0; height: 0; border: none; display: none;");
}
var form = document.createElement('form');
form.action = url;
form.method = 'POST';
form.target = iframe.name;
addParamsToForm(form, params);
iframe.appendChild(form);
document.getElementsByTagName('body')[0].appendChild(iframe);
form.submit();
}
example usage:
makePost('yourserver', {'someAttr':'someAttrValue', 'someOtherAttr': 'someOtherAttrValue'});
Or a jquery variant:
$.ajax({
type: 'POST',
url: 'yourserver',
crossDomain: true,
data: {'someAttr':'someAttrValue', 'someOtherAttr': 'someOtherAttrValue'},
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
On tips how to configure your server to support CORS:
http://enable-cors.org/server.html
Take a look at this one:
How do I send a cross-domain POST request via JavaScript?