简体   繁体   English

无法从本地域URL(Django静态文件)的文件中获取JSON数据

[英]Can't get JSON data from file from local domain URL (Django static file)

I want to load data from a JSON file to use with d3.js 我想从JSON文件加载数据以与d3.js一起使用

I'm using Django and the URL of the the JSON file json_path looks like this: /staticfile/example.json . 我正在使用Django,JSON文件json_path的URL如下所示: /staticfile/example.json I'm using the following code to read the data and do things: 我正在使用以下代码来读取数据并执行操作:

d3.json(json_path, function(error, data){
    // Do stuff with data
}

Everything works fine if I use the local IP of the server on the browser: 192.168.xx . 如果我在浏览器上使用服务器的本地IP,则一切正常: 192.168.xx

However, when I use the local domain foo.com that points to 192.168.xx , I can't load the data anymore from d3.json() . 但是,当我使用指向192.168.xx的本地域foo.com ,无法再从d3.json()加载数据。 data is null and I can't understand the content of error . data为空,我无法理解error的内容。

I'm missing something obvious probably related to callbacks or something but I have trouble understanding the big picture. 我缺少一些可能与回调有关的显而易见的东西,但我在理解全局时遇到了麻烦。 The whole Django server works perfectly well from both 192.168.xx and foo.com and everything is local. 整个Django服务器在192.168.xxfoo.com均可完美运行,并且一切都是本地的。 Any ideas? 有任何想法吗?

error is: error是:

XMLHttpRequest​mozAnon: false
mozSystem: false
​onabort: null
​onerror: function respond()​
onload: function respond()​
onloadend: null
​onloadstart: null
onprogress: function onprogress()​
onreadystatechange: null​
ontimeout: null​
readyState: 4
​response: ""​
responseText: ""
​responseType: ""​
responseURL: ""
​responseXML: null
​status: 0
statusText: ""
​timeout: 0​
upload: XMLHttpRequestUpload { onloadstart: null, onprogress: null, onabort: null, … }​
withCredentials: false​
<prototype>: XMLHttpRequestPrototype { open: open(), setRequestHeader: setRequestHeader(), send: send(), … } page:472:7

I'm using a library that uses D3 version 3.5.2 and can't update in this particular case. 我正在使用使用D3 3.5.2版的库,在这种特殊情况下无法更新。

In general, D3 has not prioritized really robust AJAX support the way that jQuery and other libraries have, because that's not its focus - so if you want to load a wide variety of external resources, like json data, you should probably do so with a 3rd-party library that has more support for carefully tweaked AJAX calls. 通常,D3并没有像jQuery和其他库那样优先考虑真正强大的AJAX支持,因为这并不是它的重点-因此,如果您要加载各种外部资源(例如json数据),则应该使用第三方库,它具有对精心调整的AJAX调用的更多支持。

Or simply using XMLHttpRequest wrapped in a makeRequest function: 或者简单地使用包装在makeRequest函数中的XMLHttpRequest:

function makeRequest(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", json_path, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadyStatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            callback(JSON.parse(xhr.response));
        }
    };
    xhr.onerror = function(error) {
        throw error;
    }
    xhr.send(); 
};

makeRequest(json_path, function(json_response) {
    //here you can use D3 to load your json as you want
    console.log(json_response);
});

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

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