leedstyh
V2EX  ›  问与答

有使用 d3.js 的吗,请教 2 个问题

  •  
  •   leedstyh · Apr 17, 2017 · 2422 views
    This topic created in 3339 days ago, the information mentioned may be changed or developed.
    1. 用 d3.csv()请求文件时,跨域的时候,如何带上 cookie ?

    2. IE/Edge 下为什么我请求的 csv 文件有问题, float 的都是 NaN , string 的都是乱码, Chrome 下正常

    4 replies    2017-04-18 06:42:26 +08:00
    msg7086
        1
    msg7086  
       Apr 18, 2017   ❤️ 1
    跨域带 Cookie 本身不是会导致跨站攻击吗?
    C18H27NO3
        2
    C18H27NO3  
       Apr 18, 2017   ❤️ 1
    1. 先用 d3-request 做个 http request , request 里 header 加上需要的 authentication ,再对返回值做 d3.csv parse
    d3.request(url)
    .header("Authenticate",token)
    .mimeType("text/csv")
    .response(function(xhr){return d3.csvParse(xhr.responseText, row);})
    [.get(callback)]

    2. 你用的是 3.X 还是 4.0 的 d3 ? 如果 chrome 里正常的话应该是 request 的问题
    leedstyh
        3
    leedstyh  
    OP
       Apr 18, 2017
    @msg7086 在 server 端设置 cors 相关的 header 就没事了吧。


    @C18H27NO3

    1 的问题,我是想加上 cookie ,不是加个 auth token 的 header ,不过我现在用 axios 重新写了一下,反正项目中其他 ajax 相关的都是用 axios 写的,这样正好不用 d3-request 了,省了个依赖。

    2 的问题,我在 server 端没有添加`text/csv`的`Content_type`,添加上之后, IE/Edge 下正常了,不添加是不规范吧,反正 chrome 没问题。
    leedstyh
        4
    leedstyh  
    OP
       Apr 18, 2017
    Content-Type
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3363 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 267ms · UTC 12:44 · PVG 20:44 · LAX 05:44 · JFK 08:44
    ♥ Do have faith in what you're doing.