I'm delving into the world of ajax requests with d3.js (v5) and I'm facing a little hiccup. Here's my code snippet:
d3.json(uri).then(data =>console.log(data));
When I tried this in an app utilizing cookie authentication, I consistently received 401 status codes. Upon inspecting the chrome dev tools, it became evident that the request was being sent without any cookies at all.
This puzzled me because vanilla javascript ajax requests normally include cookies by default. As seen in this simple example below:
function nativeAjax(uri, callback) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState === 4) {
callback(request);
}
}
request.open('get', uri, true);
request.send(null);
}
nativeAjax(uri, request => console.log(request.status));
Surprisingly, inserting this piece of code into my app displayed that the authentication cookie was indeed sent with the request, resulting in a 200
status which indicated successful authentication.
Now, here are my inquiries:
- How can I configure
d3.json
to send the necessary cookie? - How can I differentiate responses based on their status? For instance, handling a 401 response differently than a 403 response.
- Where can I find comprehensive documentation or examples for effectively using d3.json? The current documentation seems scarce and outdated resources don't apply anymore.