通过JSON读取URL

时间:2013-07-23 11:08:23

标签: javascript json

我的函数使用此URL ('..../default/call/json/mytables')

返回表的名称

但我不知道如何在我的函数getTable()中说出它来返回我的网址的值。


    function getTable(){
        return '.../default/call/json/mytables';
    }

    console.log(getTable());


    function initialize() {
            var $newDiv = $('<div>').attr('id','chart_div');
        $('#reportingContainer').append($newDiv);
  // Replace the data source URL on next line with your data source URL.
  // Specify that we want to use the XmlHttpRequest object to make the query.
  var query = new google.visualization.Query('/datasource?table='+getTable());

  // Optional request to return only column C and the sum of column B, grouped by C members.
  query.setQuery('select zone_name, sum(cost) group by zone_name');

  // Send the query with a callback function.
  query.send(drawChart);
}

感谢。

1 个答案:

答案 0 :(得分:0)

由于您返回的URL中只有JSON,请尝试以下操作:

function getTable() {
    $.ajax({
        type: 'GET',
        url: '../default/call/json/mytables',
        success: function(response) {
            // You can manipulate the variable response
            // Success!
            return response;
        },
        error: function(response) {
            // You can manipulate the variable response
            // Errors!
        }
    });
}

或者如果你想做更短的事情,你可以这样做:

function getTable() {
    return $.get('../default/call/json/mytables');
}

我更喜欢ajax方法,但要么工作!我希望能回答你的要求。