2012-04-26 80 views
1

我正在處理Windows桌面應用程序,它包含System :: Windows :: Forms :: WebBrowser類型的對象。應用程序處理一些性能數據並顯示包含各種圖形(直方圖,餅圖等)的HTML頁面。JSON.parse()默默地失敗

其工作原理如下:

  • 應用程序生成
  • 應用程序的web瀏覽器對象導航本地臨時路徑上的HTML文件(例如,... /本地/路/ myfile.html)到所生成的文件:

    webBrowser1->導航( 「... /本地/路徑/ myfile.html」)

  • jQuery和一些其他插件(例如,jqPlot)用於創建各種曲線圖一第二myfile.html是廣告應用程式內呈現

myfile.html包含的代碼段:

<div id="frame-time-histograms"> 
    <div id="frame-time-histogram-1"> 
     <div id="frame-time-histogram-1-target-plot" class="histogram-target" style="height:250px; width:900px;"></div> 
     <div id="frame-time-histogram-1-controller-plot" class="histogram-controller" style="height:100px; width:900px;"></div> 
     <script id="frame-time-histogram-1-data" class="histogram-data" type="text/plain"> 
      [{"type" : "Type 1", "shortest" : 12, "longest" : 74}, [[0, 0], [1, 12.632], [2, 16.619], [3, 16.592], [4, 16.664], [5, 16.586]]] 
     </script> 
    </div> 
    <div id="frame-time-histogram-2"> 
     <div id="frame-time-histogram-2-target-plot" class="histogram-target" style="height:250px; width:900px;"></div> 
     <div id="frame-time-histogram-2-controller-plot" class="histogram-controller" style="height:100px; width:900px;"></div> 
     <script id="frame-time-histogram-2-data" class="histogram-data" type="text/plain"> 
      [{"type" : "Type 2", "shortest" : 24, "longest" : 19}, [[0, 0], [1, 20.145], [2, 20.091], [3, 20.301], [4, 20.109], [5, 20.087]]] 
     </script> 
    </div> 
</div> 

注:這裏我使用腳本標記作爲數據容器我的直方圖。

我的JavaScript文件中包含的代碼段:

var histograms = $('div#frame-time-histograms'); 
histograms.children().each(function(index) { 
    var histogramTargetId = $(this).find('div.histogram-target').attr('id'); 
    var histogramControllerId = $(this).find('div.histogram-controller').attr('id'); 

    var histogramData = JSON.parse($(this).find('script.histogram-data').html()); 

然而JSON.parse()來似乎並沒有做任何事情。我在這行之前和之後添加了alert(「hello」),但只執行了第一行。

如果我轉到臨時路徑並雙擊myfile.html,JSON.parse()工作正常。我可以在我的網絡瀏覽器中看到所有圖表(Chrome,FF和IE)。

有人能告訴我我做錯了什麼嗎?

+0

如果只有第一個'alert'被稱爲那麼問題是,JSON失敗。有沒有某種JS​​ON庫可用和工作? – Kirstein 2012-04-26 09:40:51

+0

@Kirstein - 感謝您的回覆。事實證明,我在應用程序中使用的WebBrowser是 jpen 2012-04-26 10:17:13

回答

1

我只注意到jqPlot提供其版本json2.js文件:

...\plugins\jqplot.json2.js 

要解決我遇到的問題,我已經包含了這個js文件中myfile.html和改變

JSON.parse($(this).find('script.histogram-data').html()); 

$.jqplot.JSON.parse($(this).find('script.histogram-data').html()); 
+0

+1獲得解決方案。如果這解決了您的問題,請繼續接受您的答案。所以所有人都知道這個問題是回答。 – Boro 2012-04-27 09:06:12