简体   繁体   English

从ajax响应后调用函数

[英]calling function from ajax post response

I am using a graph library which is using javascript. 我正在使用使用JavaScript的图形 I have to run this script after getting the data with another ajax script. 在使用另一个ajax脚本获取数据后,我必须运行此脚本。 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 还有drawGraph函数是

    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. drawGraph开始等待为某些库加载google.com,但是当我使用drawGraph函数而不从ajax函数调用时,它的工作方式与我在开头提到的链接相同。

How can I call this graph function from ajax without problem ? 如何从ajax调用此图函数而不会出现问题?

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的异步属性有疑问,只需像这样修改ajax调用即可:

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

etc, etc, etc           

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM