jQuery Snippet Plugin Test
HTML
<script src="https://github.com/hleinone/jquery-snippet/raw/master/jquery.snippet.js"></script>
<ul id="list">
<li id="listItemSnippet"></li>
</ul>
<table>
<tbody id="table">
<tr id="rowSnippet" class="snippet">
<td id="cellSnippet" class="snippet"></td>
</tr>
</tbody>
</table>
JavaScript
var username = "admin";
var password = "pass";
//the first call use basic auth to get the cookie and the token
$.ajax({
url:"http://dev.host/restws/session/token",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
},
xhrFields: {
withCredentials: true
}
}).done(function(token, textStatus, jqXHR) {
console.log("got CRSF-Token:", token);
//all the subsequent calls use cookie auth+csrf token
//the cookie is handled automatically by jquery
$.ajax({
type:"POST",
url:"http://dev.host/node/",
data: '{"type":"my_node_type","title":"TEST ajax","field_description":"my content"}',
contentType: "application/json",
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRF-Token", token);
},
xhrFields: {
withCredentials: true
}
}).done(function(response, textStatus, jqXHR) {
console.log("node created:",response);
});
});