jQuery load Google Visualization API with AJAX

后端 未结 10 2088
栀梦
栀梦 2020-12-24 02:17

There is an issue that I cannot solve, I\'ve been looking a lot in the internet but found nothing.

I have this JavaScript that is used to do an Ajax request by PHP.

相关标签:
10条回答
  • 2020-12-24 02:49

    Could you provide a sample of the data returned ? You may call directly drawData(html) :

    $.ajax({
    type: "POST",
    async: false,
    url: "getTIER1Tickets.php",
    data: "",
    success: function(html){
        // Succesful, load visualization API and send data      
        google.load('visualization', '1', {'packages': ['annotatedtimeline']}); 
       //You are already in a callback function, better like this ? 
        drawData(html);                                                   
    }}); 
    
    0 讨论(0)
  • 2020-12-24 02:52

    i am not familiar at all with the google apis, but i guess that 'html' in the callback is actually json or script, you can try the $.ajax 'dataType' option:

    $.ajax({
        type: "POST",
        url: "getTIER1Tickets.php",
        dataType: "json",//"script"
        data: "",
        success: function(html){
            // Succesful, load visualization API and send data      
            google.load('visualization', '1', {'packages': ['annotatedtimeline']}); 
            google.setOnLoadCallback(drawData(html));                                                   
        }
    });
    

    more info: http://api.jquery.com/jQuery.ajax/

    0 讨论(0)
  • 2020-12-24 02:58

    I remember when I used a Google API it explicitly said to initialize the load first off. So maybe keep the google.load function out of the AJAX, and then just keep calling the second part of your function on success:

    //Straight Away!
    google.load('visualization', '1', {'packages': ['annotatedtimeline']}); 
    
    $(document).ready(function(){
        // Get TIER1Tickets                 
        $("#divTendency").addClass("loading");
    
        $.ajax({
            type: "POST",
            url: "getTIER1Tickets.php",
            data: "",
            success: function(html){
                // Succesful, load visualization API and send data
                google.setOnLoadCallback(drawData(html)); 
            }
        });  
    });
    
    0 讨论(0)
  • 2020-12-24 03:03

    I know this is an older post, but after some digging through the google.load docs, I found an async option in case you want to dynamically load the libs:

    http://code.google.com/apis/loader/

    function loadMaps() {
       google.load("visualization", "2", {"callback" : function(){ alert(4); }});
    }
    
    0 讨论(0)
  • 2020-12-24 03:04

    I know this is an old thread but this might help others.
    I've run into the same problem now and it is very similar (if not the same) I had earlier with a CMS:

    code on page:

    <div id='targetdiv'></div>
    <script type="text/javascript">
    $(document).ready( function () {
       $('#targetdiv').load('...some url...');
    });
    </script>
    

    part of the script loaded with ajax:

    <script type="text/javascript">
      document.write("hello");
    </script>
    

    The result is a page with the text "hello" that looks like it is still being loaded. This is caused by the document.write method. Since the script is loaded into an already finished and closed document, the browser opens a new one and I suppose the javascript engine is waiting for the next line of code that will never arrive as opening a new document deleted the one being executed.

    0 讨论(0)
  • 2020-12-24 03:04

    Have you tried doing this as a synchronous AJAX request? Notice the async setting below.

    $.ajax({
        type: "POST",
        async: false,
        url: "getTIER1Tickets.php",
        data: "",
        success: function(html){
            // Succesful, load visualization API and send data      
            google.load('visualization', '1', {'packages': ['annotatedtimeline']}); 
            google.setOnLoadCallback(drawData(html));                                                   
        }
    }); 
    
    0 讨论(0)
提交回复
热议问题