简体   繁体   English

d3.json为什么不随请求发送cookie?

[英]Why doesn't d3.json send cookies with the request?

I'm new to doing ajax requests with the built-in methods in d3.js (v5). 我是使用d3.js(v5)中的内置方法进行ajax请求的新手。 Here's my code: 这是我的代码:

d3.json(uri).then(data =>console.log(data));

I tried this in an app that uses cookie authentication, and kept getting 401 status codes. 我在使用Cookie身份验证的应用程序中尝试了此操作,并不断获取401状态代码。 Using the chrome dev tools revealed that it's sending the request without any cookies at all. 使用chrome dev工具显示,它发送的请求完全没有任何cookie。

That's weird because ajax requests in native javascript send cookies along with every request by default. 这很奇怪,因为默认情况下,本机javascript中的ajax请求会与所有请求一起发送cookie。 Here's an example of an ajax request in native javascript: 这是原生javascript中的ajax请求示例:

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));

Plugging this into my app, chrome dev tools shows it does send the authentication cookie along with the request and indeed the request.status comes back as 200 showing that it is indeed authenticated. chrome dev工具将其插入我的应用程序,显示它确实将身份验证cookie与请求一起发送,并且实际上是request.status返回200表明它确实已被身份验证。

My questions are: 我的问题是:

  1. how can I configure d3.json to send the required cookie? 如何配置d3.json发送所需的cookie?
  2. how do I catch the response by status? 如何按状态捕获响应? I'm going to want to do something different for 401 response than 403 response, for example. 例如,我要对401响应和403响应做一些不同的事情。
  3. Where can I read a more complete documentation or examples for how to do use d3.json? 在哪里可以阅读有关如何使用d3.json的更完整的文档或示例? I've always done native ajax because I don't use jquery, but if it's part of the library I'm using anyway, I'd like to learn how to use it. 我一直都做本机Ajax,因为我不使用jquery,但是无论如何,如果它是我正在使用的库的一部分,我想学习如何使用它。 But the documentation says almost nothing about it, and the page it links to isn't helpful either. 但是文档对此几乎没有说明,并且链接到页面也没有帮助。 And most tutorials were for previous versions of d3 and don't work anymore. 而且大多数教程都是针对d3的早期版本的,不再起作用。

Version 5 switched to use the Fetch API, which doesn't send any cookies by default. 版本5切换为使用Fetch API,该API默认情况下不发送任何cookie。 You can overcome this by adding options for d3.json to pass through to fetch: 您可以通过添加d3.json传递给fetch的选项来克服这一问题:

d3.json(uri, {credentials: "same-origin"}).then(...);

Or, for cross-origin requests: 或者,对于跨域请求:

d3.json(uri, {credentials: "include"}).then(...);

If a 4XX status (or 5XX) is returned D3 will cause the promise to reject, which you can handle by providing a second callback function to then . 如果返回4xx状态(或5XX)D3将导致承诺拒绝,您可以通过提供第二个消息处理函数then I welcome corrections, but I believe there is no way within this function to get the actual status code. 我欢迎进行更正,但我相信此函数无法获取实际的状态代码。

The only mention of the change to Fetch and promises I found in the documentation (at the time of writing) was in the changelog: D3 v5 Changes . 我在文档中(在撰写本文时)仅发现了对Fetch和promises 所做的更改 ,该更改记录是在changelog中: D3 v5 Changes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM