Skip to content

Instantly share code, notes, and snippets.

@joaoneto
Created April 29, 2013 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaoneto/5479938 to your computer and use it in GitHub Desktop.
Save joaoneto/5479938 to your computer and use it in GitHub Desktop.
jQuery ajax with CORS
<!doctype html>
<html>
<header>
<title>Dev tools</title>
</header>
<body>
<h1>Dev tools</h1>
<div data-scope="main"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="socket.io.min.js"></script>
<script type="text/javascript">
(function (window, document, $, io) {
'use strict';
$.support.cors = true;
var xhrCorsFields = {
withCredentials: true
};
function corsReq(options, data, type, callback) {
var settings = $.extend({}, $.ajaxSettings, { xhrFields: xhrCorsFields });
settings.crossDomain = true;
if ('string' === typeof options) settings.url = options;
else settings = $.extend(settings, options);
if ('string' === typeof data) settings.type = data;
else if (data) settings.data = data;
if ('function' === typeof type) settings.complete = type;
else if (type) settings.type = type;
if (callback) settings.complete = callback;
console.log(settings);
return $.ajax(settings);
}
// corsReq('http://localhost:3000/v1/sessions', { email: 'user@example.com', password: '123' }, 'POST', function (res) {
// console.log(res);
// });
window.corsReq = corsReq;
$(document).ready(function () {
});
})(window, document, window.jQuery, window.io);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment