dongmian8108 2014-12-14 21:07
浏览 45
已采纳

是否需要从服务器检索数据的SQL数据库?

Sorry but I'm a newbie to web development. I've been playing around with some JSON objects lately and have been using some example from W3school site - 'http://www.w3schools.com/json/json_example.asp.'

The example retrieves data from MYSQLi db and injected it into a JSON object. I've been using this example to create a dynamic site however my problem is I'm not sure if you need a db to store the data in the first place. Can JSON object be directly retrieved from a server?

The response from the server is parsed as a JSON object. i.e. var = JSON.parse(response)

<!DOCTYPE html>

<html>

<head>

<title>XMLHTTPREQUESTEXAMPLE</title>

</head>

<body>

<p id="id01"></p>

<script>

var xmlhttp = new XMLHttpRequest();

var url = "//http:URL";

xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        myFunction(xmlhttp.responseText);

    }else{

                    document.getElementById("id01").innerHTML = "Server not responding";

                }

}

xmlhttp.open("GET", url, true);

xmlhttp.send();



function myFunction(response) {

    var arr = JSON.parse(response);

    var i;

    var out = "<table><tr><th>Name</th><th>Job Title</th><th>Room No</th><th>Tel Ext</th><th>Email</th></tr>";



    for(i = 0; i < arr.length; i++) {

        out += "<tr><td>" + 

        arr[i].Name +

        "</td><td>" +

        arr[i].JobTitle +

        "</td><td>" +

        arr[i].RoomNo +

        "</td></tr>"

                                arr[i].TelExt +

        "</td></tr>"

                                arr[i].Email +

        "</td></tr>";

    }

    out += "</table>"

    document.getElementById("id01").innerHTML = out;

}

</script>

</body>

</html>

The example php script on the server from the W3 school site.

$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");

$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
    $outp .= '"City":"'   . $rs["City"]        . '",';
    $outp .= '"Country":"'. $rs["Country"]     . '"}'; 
}
$outp .="]";

$conn->close();

echo($outp);
?>
  • 写回答

3条回答 默认 最新

  • dongzhong2674 2014-12-14 21:17
    关注

    That example uses a MySQL database to retrieve data, but you can simply echo out your own data just to test your AJAX with JSON example like so:

    <?php
    
    $array = array(
    "Key" => "value",
    "Foo" => "Bar"
    );
    
    $jsonData = json_encode($array);
    
    echo ($jsonData);
    
    ?>
    

    Then you can retrieve the values in your Javascript's myFunction by doing :

    arr.Foo; //would return Bar
    

    A database is just for storing long-term data, it is not needed for the functional part of this tutorial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法