Jump to content

CORS


Matej

Recommended Posts

Yo , i just came to "Cross-origin-resource sharing" . I have read articles about in on MDN and html5rocks but im not sure i get it . "Cross-origin-resource sharing basicly means that i want to get some content from other site which aint ony my domain (for example site w3schools.com wants some data from codeadacemy.com) using ajax.

But here comes the usage . I only found this way

function createCORSRequest(method, url) {var xhr = new XMLHttpRequest();if ("withCredentials" in xhr) {// XHR for Chrome/Firefox/Opera/Safari.xhr.open(method, url, true);} else if (typeof XDomainRequest != "undefined") {// XDomainRequest for IE.xhr = new XDomainRequest();xhr.open(method, url);} else {// CORS not supported.xhr = null;}return xhr;}// Make the actual CORS request.function makeCorsRequest() {// All HTML5 Rocks properties support CORS.var url = 'http://updates.html5rocks.com';var xhr = createCORSRequest('GET', url);if (!xhr) {alert('CORS not supported');return;}// Response handlers.xhr.onload = function() {var text = xhr.responseText;var title = getTitle(text);alert('Response from CORS request to ' + url + ': ' + title);};xhr.onerror = function() {alert('Woops, there was an error making the request.');};xhr.send();}

Is it really that simple or am i missing something?

And, what does "handling not-so-simple request " mean?On html5 rocks there is an example

var url = 'http://api.alice.com/cors';var xhr = createCORSRequest('PUT', url);xhr.setRequestHeader('X-Custom-Header', 'value');xhr.send();

What is this "handling" used for? I cant find any real life examples or nice explanationThanks for answers and have a nice day.

Edited by Matej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...