douzhaiyuan1731 2015-05-18 14:44
浏览 97
已采纳

将Javascript数据(JSON)保存到数据库中[关闭]

I have a table as you can see in the Link here: http://csgo.nssgaming.com/index.php This is a list with data there is automatic updated, but instead of outputting this information in a table, i would like to have it into a database. but i have no idea how to do it, when it is in javascript. so i would be happy if someone could tell me what to do or show a example.

you can see the code here:

<!DOCTYPE HTML>
<!-- Install gentoo fgt -->
<html>
<head>
    <title>csgolounge-api example</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css"  href="http://bootswatch.com/darkly/bootstrap.min.css" />
</head>
<body>
    <div class="content">
        <div class="container">
            <nav class="navbar navbar-default" role="navigation" style="margin-top: 20px;">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <a class="navbar-brand" href="#">lel</a>
                    </div>
                </div>
            </nav>
            <div class="row">
                <div class="col-md-12">
                    <div class="well" id="radio_2">
                        <legend>Matches</legend>
                        <table class="table table-striped table-bordered table-condensed">
                            <thead>
                                <tr>
                                    <th>Team_1</th>
                                    <th>Team_2</th>
                                    <th>Time</th>
                                    <th>Status</th>
                                    <th>Link->csgolounge</th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script>
        function sortTable(table, order) {
            var asc   = order === 'asc',
                tbody = table.find('tbody');

            tbody.find('tr').sort(function(a, b) {
                if (asc) return $('td:last', a).text().localeCompare($('td:last', b).text());
                else return $('td:last', b).text().localeCompare($('td:last', a).text());
            }).appendTo(tbody);
        }

        $.getJSON("./api.php", function(data) {
            $.each(data, function(key, val) {
                var live, time, team_1, team_2

                live = val.live
                time = val.time
                team_1 = val.teams[0].name + " (" + val.teams[0].percent + ")"
                team_2 = val.teams[1].name + " (" + val.teams[1].percent + ")"
                link = key
                status = live ? "Live" : ""

                if(val.result)
                    if(val.result.status == "won")
                        if(val.result.team == 0) team_1 += " [won]"
                        else team_2 += " [won]"

                $("tbody").html($("tbody").html() + "<tr><td>" + team_1 + "</td><td>" + team_2 + "</td><td>" + time + "</td><td>" + status + "</td><td>" + link + "</td></tr>")
                });

            sortTable($('table'),'desc');
        });
    </script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douyanyan1123 2015-05-18 15:00
    关注

    You mean you got data from the API and you would like to save it in you database?

    Then, instead of $.getJSON, you can use $.ajax. It makes you able to send the data. It will forward data to your database.

    $.getJSON("./api.php", function(data) {
    
      $.ajax({
        dataType: "json",
        method : "POST",
        url: 'database.php',
        data: data,
        success: function(){
          console.log('Data has been sent to the server!')
        }
      });
    
      $.each(data, function(key, val) {
        //... fill your table ... 
      });
      sortTable($('table'),'desc');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果