由于一个完整的购物商城代码涉及到很多方面(如前端、后端、数据库等),并且由于篇幅限制,我无法直接给出所有电脑语言的完整代码。但我可以为你提供一个简化的概念性示例,分别用 Python(后端)和 HTML/JavaScript(前端)来说明。
后端(Python,使用 Flask 框架)
python
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
# 假设有一个简单的商品列表
products = [
{"id": 1, "name": "产品A", "price": 100},
{"id": 2, "name": "产品B", "price": 200},
# ...更多产品
]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/products', methods=['GET'])
def get_products():
return jsonify(products)
# 其他路由和逻辑...
if __name__ == '__main__':
app.run(debug=True)
前端(HTML/JavaScript)
index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物商城</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON('/products', function(data) {
let html = '';
data.forEach(function(product) {
html += `<div>${product.name} - 价格: ${product.price}</div>`;
});
$('#products').html(html);
});
});
</script>
</head>
<body>
<h1>欢迎来到购物商城</h1>
<div id="products"></div>
<!-- 其他页面内容... -->
</body>
</html>