Skip to content

Instantly share code, notes, and snippets.

@asiletto
Last active August 29, 2015 14:02
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 asiletto/5423748c5d007aca6364 to your computer and use it in GitHub Desktop.
Save asiletto/5423748c5d007aca6364 to your computer and use it in GitHub Desktop.
jquery/Drupal/restws CORS example
var username = "admin";
var password = "passw";
console.log("authentication...")
$.ajax({
url:"http://my.host/restws/session/token-cors",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
},
xhrFields: {
withCredentials: true
}
})
.done(function(token, textStatus, jqXHR) {
//jquery has set the auth cookie now
console.log("read token:", token);
console.log("inserting node...");
$.ajax({
type:"POST",
url:"http://my.host/node/",
data: '{"type":"my_content_type","title":"TEST ajax","field_description":"some content of the description field"}',
contentType: "application/json",
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRF-Token", token);
},
xhrFields: {
withCredentials: true
}
}).done(function(response, textStatus, jqXHR) {
console.log("response:",response);
console.log("textStatus:",textStatus);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("AJAX error inserting node: " + textStatus + ' : ' + errorThrown);
});
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("AJAX error during authentication: " + textStatus + ' : ' + errorThrown);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment