gpt4 book ai didi

html - 使用Vue Javascript将JSON数据提取到html文件中的表中时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:52 25 4
gpt4 key购买 nike

这也是我的 HTML 文件,其中包含 Vue Js 代码:

    <!DOCTYPE html>
<html>

<head>

<!-- Vue development version, includes helpful console warnings -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<!-- Axios library -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>

<body>

<div id="app">
<table>
<thead>
<tr>
<th>name</th>
<th>description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="game in games">
<td>{{game.Name}}</td>
<td>{{game.Description}}</td>
<td>{{game.price}}</td>
</tr>
</tbody>
</table>
</div>

<script>

const app = new Vue({
el: '#app',
data: {
games: []
},
methods : {
//get all the products from the web service using Axios
loadAllProducts: function(){
var localApp = this;
axios.get('/finalgame') //send GET request to games path
.then(function (response){
//returned array of games
localApp.games = response.data.data;
console.log(JSON.stringify(response.data.data));
})
.catch(function (error){
//handle error
console.log(error);
});
}
},
created: function(){
this.loadAllProducts();

//refreshes every 5 seconds
setInterval(this.loadAllProducts, 4000);
}
})

</script>

我的 Node JS server.js 文件:

//Import the express and url modules
var express = require('express');
var url = require("url");

//Status codes defined in external file
require('./http_status');

//The express module is a function. When it is executed it returns an app object
var app = express();

//Import the mysql module
var mysql = require('mysql');

//use index html page
app.use(express.static('./public'))

//Start the app listening on port 8080
app.listen(8080);

//Create a connection object with the user details
var connectionPool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "root",
password: "",
database: "pricen",
debug: false
});

app.get('/messages', (req, res) => {
console.log("show messages")
res.end()
})

app.get('/index.html', (req, res) =>{
res.sendFile(__dirname + '/index.html');
})


//URL to get all game products with price
app.get('/finalgame', (req, res) => {
console.log("Fetching game product with prices by joining table: " + req.params.id)
const sqlquery = "SELECT name, description, price FROM game" + " INNER JOIN price ON game.id = game_id"
connectionPool.query(sqlquery, (err, rows, fields) => {
console.log("user success!")
res.json(rows)
})
})

当您访问 localhost:8080/finalgame 时,会给您以下数据:

[{"name":"Fifa 19...","description":"Fifa 19 game","price":29.85}, 
{"name":"Fifa 20...","description":"Fifa 20 game","price":29.85},
{"name":"name":"Far Cry 4...","description":"Far Cry 4...","price":14.85}]

我想使用 vue js 将此 JSON 数据逐行显示到我的表中,因为显然与使用 AJAX 的 HTTPRequest 相比,它更容易使用。当我转到 localhost:8080/index.html 时得到的输出:

名称 描述 价格我已经坚持了好几天了。请帮忙。

最佳答案

如果您返回的内容看起来像...

[{"name":"Fifa 19...","description":"Fifa 19 game","price":29.85}.....]

这意味着没有data属性,但你得到的是一个数组。所以尝试一下:

.then(function (response){
// remove the extra 'data'
localApp.games = response.data;
console.log(JSON.stringify(response.data));
})

然后像其他答案一样,检查外壳。您的回复中包含小写字母,因此应该是

{{ game.name }}{{game.description}}

关于html - 使用Vue Javascript将JSON数据提取到html文件中的表中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59306306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com