doushi1929 2014-06-13 18:49
浏览 76
已采纳

从ajax post响应中调用函数

I am using a graph library which is using javascript. I have to run this script after getting the data with another ajax script. But when I use the script

    var request, response;

    var newUrl = url + '/thermometer/getLastHourData';
    var data =  $.ajax({
                    url: newUrl,
                    data: request,
                    dataType: "json",
                    method: "post",
                    success: function(data) {
                        drawGraph();
                        return data;
                    },
                    error: function() {
                        alert('Error occured');
                    }
            });

Also the drawGraph function is

    google.load("visualization", "1");

    // Set callback to run when API is loaded
    google.setOnLoadCallback(drawVisualization);

    // Called when the Visualization API is loaded.
    function drawVisualization() {
        // Create and populate a data table.
        var data = new google.visualization.DataTable();
        data.addColumn('datetime', 'time');
        data.addColumn('number', 'Function A');
        data.addColumn('number', 'Function B');

        function functionA(x) {
            return Math.sin(x / 25) * Math.cos(x / 25) * 50 + (Math.random()-0.5) * 10;
        }

        function functionB(x) {
            return Math.sin(x / 50) *50 + Math.cos(x / 7) * 75 + (Math.random()-0.5) * 20 + 20;
        }

        // create data
        var d = new Date(2010, 9, 23, 20, 0, 0);
        for (var i = 0; i < 100; i++) {
            data.addRow([new Date(d), functionA(i), functionB(i)]);
            d.setMinutes(d.getMinutes() + 1);
        }

        // specify options
        var options = {
            "width":  "100%",
            "height": "350px"
        };

        // Instantiate our graph object.
        var graph = new links.Graph(document.getElementById('mygraph'));

        // Draw our graph with the created data and options
        graph.draw(data, options);
    }

(I did not change it what I need to since checking the code is running correctly)

drawGraph start to wait for loading google.com for some library but when I use the function drawGraph without calling from the ajax function, it works as in the link that I mention at the begining.

How can I call this graph function from ajax without problem ?

  • 写回答

1条回答 默认 最新

  • douqian5920 2014-06-13 18:57
    关注

    I'm not at all clear what you question is here. However if you are having problems with the asynchronous properties of ajax, simply modify your ajax call like this:

    $.ajax({
          url: newUrl,
          data: request,
          async: false,
    
    etc, etc, etc           
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码