Skip to content

Instantly share code, notes, and snippets.

@EddyRespondek
Created October 22, 2014 22:21
Show Gist options
  • Save EddyRespondek/58844e2afe6bbea57273 to your computer and use it in GitHub Desktop.
Save EddyRespondek/58844e2afe6bbea57273 to your computer and use it in GitHub Desktop.
<html>
<head>
<title></title>
<script src="jquery.min.js"></script>
<script type='text/javascript' src="http://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.1/jquery.xdomainrequest.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script type="text/javascript">
function CalculateSig(stringToSign, privateKey){
var hash = CryptoJS.HmacSHA1(stringToSign, privateKey);
var base64 = hash.toString(CryptoJS.enc.Base64);
return encodeURIComponent(base64);
}
$(document).ready(function() {
var d = new Date,
expiration = 3600 // 1 hour,
unixtime = parseInt(d.getTime() / 1000),
future_unixtime = unixtime + expiration,
publicKey = "xxxxxx",
privateKey = "xxxxxx",
method = "GET",
route = "forms/24";
stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime;
sig = CalculateSig(stringToSign, privateKey);
url = 'https://www.you-domain.com/gravityformsapi/forms/24/?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime;
console.log(url);
var contentType ="application/x-www-form-urlencoded; charset=utf-8";
if(window.XDomainRequest) {
contentType = "text/plain";
}
$("#postdata").click(function() {
$.ajax({
url: url,
crossDomain: true,
data: "name=Ravi&age=12",
type: "POST",
dataType: "json",
contentType: contentType,
success:function(data) {
alert("Data from Server"+JSON.stringify(data));
},
error:function(jqXHR,textStatus,errorThrown) {
alert("You can not send Cross Domain AJAX requests: "+errorThrown);
}
});
});
//$("#getdata").click(function() {
$.ajax({
url: url,
crossDomain: true,
data: "name=Ravi&age=12",
type: "GET",
dataType: "json",
contentType: contentType,
success:function(data) {
alert("Data from Server"+JSON.stringify(data));
},
error:function(jqXHR,textStatus,errorThrown) {
alert("You can not send Cross Domain AJAX requests : "+errorThrown);
}
});
//});
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment