如何上传带有HTTP标头的CSV文件

时间:2015-12-10 09:17:02

标签: csv header pykcharts

我正在尝试使用pykcharts的象形文字 我在检索我的csv文件时遇到了问题。

这是我得到的错误:

  

XMLHttpRequest无法加载   http://我的域名/cust_pictograph.csv。没有   请求中存在“Access-Control-Allow-Origin”标头   资源。因此,不允许来源“http:// localhost”访问。

  <script>
   window.PykChartsInit = function (e) {
   var k = new PykCharts.other.pictograph({

      //Chart Container Id
      "selector": "#my_chart",
    
      //Data file path
      "data": "http://my domain/cust_pictograph.csv",
      

      //Chart Size Parameters
      "chart_width": 100,
      "chart_height": 100,
    
      //Realtime data parameters
      "real_time_charts_last_updated_at_enable": "no",
      "real_time_charts_refresh_frequency": 0,
    
      //Chart-interactive parameters
      "transition_duration": 0,
    
      //Other parameters
      "pictograph_total_count_enable": "no",
      "pictograph_current_count_enable": "no",
      "pictograph_image_per_line": 3,
      "pictograph_image_width": 30,
      "pictograph_image_height": 30,
      "pictograph_total_count_size": 20,
      "pictograph_total_count_weight": "normal",
      "pictograph_total_count_family": "Helvetica Neue,Helvetica,Arial,sans-serif",
      "pictograph_total_count_color": "grey",
      "pictograph_current_count_size": 20,
      "pictograph_current_count_weight": "normal",
      "pictograph_current_count_family": "Helvetica Neue,Helvetica,Arial,sans-serif",
      "pictograph_current_count_color": "#255AEE",
      "pictograph_units_per_image_text_size": 10,
      "pictograph_units_per_image_text_color": "grey",
      "pictograph_units_per_image_text_family": "Helvetica Neue,Helvetica,Arial,sans-serif",
      "pictograph_units_per_image_text_weight": "normal",
    
      //Chart title parameters
      // "title_text": "Enter title here",
      // "title_size": 2,
      // "title_weight": "bold",
      // "title_family": "Helvetica Neue,Helvetica,Arial,sans-serif",
      // "title_color": "#1D1D1D",
    
      // //Chart subtitle parameters
      // "subtitle_text": "Enter subtitle here",
      // "subtitle_size": 2,
      // "subtitle_weight": "normal",
      // "subtitle_family": "Helvetica Neue,Helvetica,Arial,sans-serif",
      // "subtitle_color": "black",
    
      //Credits parameters
      "credit_my_site_name": "a",
      "credit_my_site_url": "a"
   });
   k.execute();
  }
   </script>

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

此错误会附加,因为您尝试从http://yourdomain.com加载http://localhost

常规网页可以使用XMLHttpRequest对象从远程服务器发送和接收数据,但它们受同源策略的限制。

了解详情:https://developer.chrome.com/extensions/xhr

您可以在http://yourdomain.com返回标题Access-Control-Allow-Origin以允许某些域:

Access-Control-Allow-Origin: http://from.domain.com

或每个域名:

Access-Control-Allow-Origin: *

但要小心,最后一个解决方案让人们可以从任何地方加载数据。

我不知道你是否可以设置为http://localhost,但你可以试试吗?

更多详情:http://www.html5rocks.com/en/tutorials/cors/

在Aspx文件中:

Response.Clear();
Response.ContentType = "text/plain";
Response.AppendHeader("Access-Control-Allow-Origin", "*");
Response.Write(File.ReadAllText(@"c:\serverpath\to\file\cust_pictograph.csv"));
Response.End();