Last active
August 29, 2015 14:02
-
-
Save fyquah/d37585b45a425841d97b to your computer and use it in GitHub Desktop.
Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script> | |
function createCORSRequest(method, url) { | |
var xhr = new XMLHttpRequest(); | |
if ("withCredentials" in xhr) { | |
// XHR for Chrome/Firefox/Opera/Safari. | |
xhr.open(method, url); | |
} else if (typeof XDomainRequest != "undefined") { | |
// XDomainRequest for IE. | |
xhr = new XDomainRequest(); | |
xhr.open(method, url); | |
} else { | |
// CORS not supported. | |
xhr = null; | |
} | |
return xhr; | |
} | |
// Helper method to parse the title tag from the response. | |
// Make the actual CORS request. | |
function makeCorsRequest() { | |
// All HTML5 Rocks properties support CORS. | |
var url = "http://proj3-fyquah.rhcloud.com/users/login"; | |
var username = "brandon"; | |
var password = "brandonbrandon"; | |
var params = "data[User][username]=" + username + "&data[User][password]=" + password; | |
// var url = "http://proj3-fyquah.rhcloud.com/messages/submit/" | |
// var token = "6222ab83c9880589c38bbfbae13419e219907473"; | |
// var message = "Hello from AJAX"; | |
// var lat = 1.000; | |
// var lng = 1.000; | |
// var radius = 10; | |
// var period = 10; | |
// var params = "data[User][access_token]=" + token + "&data[Message][message]=" + message + "&data[Message][lat]=" + lat + "&data[Message][lng]=" + lng + "&data[Message][radius]=" + radius + "&data[Message][period]=" + period; | |
// var url = 'http://proj3-fyquah.rhcloud.com/users/signup'; | |
// var username = "snowman"; | |
// var password = "09090909"; | |
// var email = "[email protected]"; | |
// var first_name = "Nobita"; | |
// var last_name = "Kun"; | |
// var params = "data[User][username]=" + username + "&data[User][password]=" + password + "&data[User][email]=" + email + "&data[User][first_name]=" + first_name + "&data[User][last_name]=" + last_name; | |
var xhr = createCORSRequest('POST', url); | |
if (!xhr) { | |
alert('CORS not supported'); | |
return; | |
} | |
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
// Response handlers. | |
xhr.onload = function() { | |
var text = xhr.responseText; | |
document.getElementById("myDiv").innerHTML = xhr.responseText; | |
}; | |
xhr.onerror = function() { | |
alert('Woops, there was an error making the request.'); | |
}; | |
xhr.send(params); | |
} | |
</script> | |
</head> | |
<body> | |
<h2>AJAX</h2> | |
<button type="button" onclick="makeCorsRequest()">Request data</button> | |
<div id="myDiv"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment