Ajax请求将Highcharts插入Head和Run函数

时间:2013-10-04 00:45:56

标签: javascript php jquery ajax highcharts

我不是最好的javascript,而且我遇到了一些麻烦。任何建议都表示赞赏。

我尝试在有人点击带有onclick事件的链接后生成图表。

我的文件的负责人看起来像这样。

<script src="billajax.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="highcharts.js"></script>
<script src="exporting.js"></script>

Billajax.js有很多功能,但为了简化,它看起来像这样。

function getl(uid){

if(document.getElementById("votescode")){


    var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse5(xmlHttp.responseText);
      //BELOW GENERATES CHART CODE
          var xmlHttp2 = getXMLHttp();
        xmlHttp2.onreadystatechange = function()
        {
        if(xmlHttp2.readyState == 4)
        {
          HandleResponse4(xmlHttp2.responseText);
          dothis();

            }
        }
        xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true); 
        xmlHttp2.send(null);

    }
  }
  xmlHttp.open("GET", "ajax.php?&p=gl&req="+uid+"&n="+Math.random(), true); 
  xmlHttp.send(null);


}else{

    var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse5(xmlHttp.responseText);

      //BELOW GENERATES CHART CODE
  var xmlHttp2 = getXMLHttp();
    xmlHttp2.onreadystatechange = function()
  {
    if(xmlHttp2.readyState == 4)
    {
      HandleResponse3(xmlHttp2.responseText);
      dothis();

    }
  }
  xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true); 
  xmlHttp2.send(null);


    }
  }
  xmlHttp.open("GET", "ajax.php?&p=gl&req="+uid+"&n="+Math.random(), true); 
  xmlHttp.send(null);



}

}

function HandleResponse3(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
var script = document.createElement("script");
script.setAttribute("id", "votescode");
script.innerHTML = response;
document.head.appendChild(script);

}
function HandleResponse4(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
var el = document.getElementById( 'votescode' ); 
el.parentNode.removeChild( el );

var script = document.createElement("script");
script.setAttribute("id", "votescode");
script.innerHTML = response;
document.head.appendChild(script);


}
function HandleResponse5(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
document.getElementById('initiative').innerHTML = response;
//var shn = $('.shtn').ThreeDots();
//shn.ThreeDots.update({max_rows:1});
//var bsy = $('.billsy').ThreeDots();
//bsy.ThreeDots.update({max_rows:3});
}

getL(uid)中ajax调用的输出如下,并成功插入到文档的头部。用HandleResponse5填充的html内容也正确插入。

单击链接后,它会触发getl(uid)函数,该函数生成插入页面的必要代码(highcharts容器),然后根据脚本是否为脚本生成HandleResponse3或Handleresponse4的高图代码。以前解雇过。

生成高图的代码输出如下。它可以工作,如果我把它扔进jsfiddle,所以我无法弄清楚为什么我一直收到以下错误(来自chrome控制台)。

Uncaught TypeError: Object [object Object] has no method 'highcharts'
dothis
(anonymous function)
o jquery.min.js:2
p.add jquery.min.js:2
e.fn.e.ready jquery.min.js:2
e.fn.e.init jquery.min.js:2
e jquery.min.js:2
(anonymous function)
HandleResponse3 billajax.js:310
xmlHttp2.onreadystatechange billajax.js:247
Uncaught TypeError: Object [object Object] has no method 'highcharts'
dothis
xmlHttp2.onreadystatechange

这是由getl(uid)中定义的ajax.php调用生成的代码

//CALL FROM BILL AJAX ABOVE WHICH GENERATES CODE BELOW.
xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true);

function dothis(){



// Radialize the colors
        Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
            return {
                radialGradient: { cx: 0.4, cy: 0.2, r: 0.7
                                },
                stops: [
                    [0, color],
                    [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
                ]
            };
        });
$('#container').highcharts({
        chart: {
            renderTo: 'container',
                 backgroundColor:'transparent',
                 width:130,height:140,
                 size:'100%'
        },
        title: {
            text: 'Browser market shares at a specific website, 2010'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                size: 200,
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }

            }
        },
        credits:{enabled:false},
        exporting:{enabled:false},
        colors:['#ADD46D','#F1744F','#b9e376','#f2a48d'],
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Supports You',   1],
                ['Opposes You',       1],
                ['Absent on Supporting',    0],
                ['Absent on Opposing',    0]
            ]
        }]
    });
 }




    function dothis2(){



// Radialize the colors
        Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
            return {
                radialGradient: { cx: 0.4, cy: 0.2, r: 0.7
                                },
                stops: [
                    [0, color],
                    [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
                ]
            };
        });
       $('#container2').highcharts({
        chart: {
            renderTo: 'container2',
                 backgroundColor:'transparent',
                 width:130,height:140,
                 size:'100%'
        },
        legend:{
            enabled: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2010'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                size: 200,
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }

            }
        },
        credits:{enabled:false},
        exporting:{enabled:false},
        colors:['#ADD46D','#F1744F','#b9e376','#f2a48d'],
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
               ['Supports You',   0],
                ['Opposes You',       2],
                ['Absent on Supporting',    0],
                ['Absent on Opposing',    0]
            ]
        }]
    });
 }



 $(function () { dothis();dothis2(); });

我已经成功实施了类似的解决方案,但我无法弄清楚为什么当我使用类似的设置时,这将无法正常工作。

同样,任何建议都会受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

问题已解决,页面正文中存在冲突的库。脚本的冲突导致了错误。