【问题标题】:Javascript not referencing local json fileJavascript未引用本地json文件
【发布时间】:2014-06-25 03:10:14
【问题描述】:

我似乎无法让我的变量引用本地 json 文件而不是当前的远程 json 文件。我希望能够将 Javascript 设置为读取本地 test.json 文件而不是远程 resturl url。我想将当前的resturl 变量更改为它下面的注释resturl。如果您需要更多信息,请告诉我。脚本基于:https://github.com/kenklin/aws-price-comparison-chart

function update() {
// Construct REST URL
 var resturl = "http://p1software-eb1.elasticbeanstalk.com/awsec2offering/api"
  + "/" + d3.select("#availabilityZone").node().value // us-east-1a
  + "/" + d3.select("#productDescription").node().value // linux
  + "/" + d3.select("#offeringType").node().value   // heavy
  + "/" + d3.select("#instanceType1").node().value    // t1.micro
  + "," + d3.select("#instanceType2").node().value;   // m1.small

// resturl = "test.json"
// resturl = "http://localhost:8080/awsec2offering/awsec2offering/api/us-east-1a/linux/heavy/t1.micro,m1.small."
d3.json(resturl, function(error, json) {
  data = json.ec2offerings;

  // Assign colors to each unique name. 
  var color = d3.scale.category10();
  color.domain(data.map(function(p) { return createUniqueName(p); }));

  // Coerce the data to numbers.
  data.forEach(function(d) {
    d.month = +d.month;
  });

【问题讨论】:

  • 您是否收到任何错误消息?
  • 没有。但是,当我更改变量时,我确实得到:中断异常:TypeError:无法读取未定义的属性“ec2offerings”
  • error 是否包含任何内容?
  • 没有。我的示例基于:github.com/kenklin/aws-price-comparison-chart

标签: javascript jquery json d3.js dart


【解决方案1】:

带有回调参数的JSONP获取跨域脚本

How to send data using JSONP from localhost to domain.com

【讨论】:

  • 我不确定在那里做什么。我没有使用 JSONP 的经验。你能提供一个基于代码的答案吗?
  • 假设您的代码在服务器 example.com 上运行,并且您想要 localhost,那么您的 localhost url 应该是:http://localhost:8080/test.json?callback=? 这也可能有效,但不太可能:file:///path/to/file/test.json?callback=?跨度>
  • 我不知道该怎么写,因为localhost:8080/test.json 似乎不起作用。它在同一个目录中。
  • 抱歉,我不得不编辑我的答案,因为我不小心一直点击enter
  • 这里是另一个参考网址:stackoverflow.com/questions/5943630/…
猜你喜欢
  • 2018-10-25
  • 2017-05-14
  • 1970-01-01
  • 2013-07-30
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
相关资源
最近更新 更多