javascript – 如何构建动态html表单远程CSV

我正在尝试建立一个表格来显示实时太阳能发电数据.可以使用以下jsp字符串检索数据.

http://pvoutput.org/service/r2/getstatistic.jsp?key=customer-key&sid=customer-id

该jsp的输出是逗号分隔的字符串,类似于以下内容:

5379600,49505,112075,0,216496,2.802,48,20100830,20151216,5.412,20151215

第一个值是能量产生,第二个是能量输出等等……
我需要拆分这些值并显示在自定义设计的HTML表单上.

我有合理的HTML知识,但很少有javascript或任何其他编程语言知识.

任何帮助将不胜感激.

最佳答案 您可以像这样使用ajax获取CSV(需要jQuery):

$(document).ready(function() {

$.ajax({
  url: "http://pvoutput.org/service/r2/getstatistic.jsp?key=customer-key&sid=customer-id",
  cache: false
})
  .done(function(csv) {
    // comma separated values can be converted to an array using split
    var dataarray = csv.split(",")
    // populate your html with the single data elements
    $("#yourform first").html(dataarray[0])
    $("#yourform second").html(dataarray[1])
    // ...
  });
}

这将一直等到你的HTML加载完毕,然后发出一个AJAX请求,并在完成请求时将结果值拆分,只是为了用它们填充你的HTML.而不是document.ready,您也可以通过单击按钮等来执行此逻辑.

点赞