在云中存储变量的最佳方式是什么?
What is the best way to go about storing variables in the cloud?
作为一个有趣的小项目,我将尝试制作一种玩具加密货币。我现在可以完成这个项目的大约 1/2,但我需要知道如何通过 js 在服务器上存储数据(即 serverstorage["amount"] = 3
)
提前致谢!
编辑: 感谢@chucky 给我这么好的答案,并最终让我承认后端的存在。
编辑 2:修正语法并解决问题
在我看来你需要某种服务器端脚本,你的 javascript 可以通过 AJAX 请求调用它来获取加密货币的价值。
服务器端脚本会 return 一个原始值,或者它会在数据库或其他存储系统中查找该值。
实现由您决定,但是一个简单的 php 文件可以 return 像这样:
<?php
setlocale(LC_MONETARY, 'en_US.UTF-8');//make sure we have USD dollar sign
$value = 3.0;//or get your value from where ever you need to
$output = money_format('%.2n', $value);
echo($output);
?>
现在 javascript 您可以向 php 脚本发出这样的请求:
function getcurrencyvalue() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("currency").innerHTML = this.responseText;
}
};
xhttp.open("GET", "currency.php", true);//the GET request should be where you put the php file.
xhttp.send();
}
以及所有将放入 html 文件(或您正在使用的文件)的所有内容:
<!DOCTYPE html>
<html>
<head>
<title>Currency Test</title>
<script src="currency.js"></script>
<script>
getcurrencyvalue();
</script>
</head>
<body>
<h1>Currency Value is:</h1>
<div id="currency">
</div>
</body>
</html>
完成所有设置后,您需要做的就是让您的客户加载该页面,对于多个用户,它始终是您 return 的值。
作为一个有趣的小项目,我将尝试制作一种玩具加密货币。我现在可以完成这个项目的大约 1/2,但我需要知道如何通过 js 在服务器上存储数据(即 serverstorage["amount"] = 3
)
提前致谢!
编辑: 感谢@chucky 给我这么好的答案,并最终让我承认后端的存在。
编辑 2:修正语法并解决问题
在我看来你需要某种服务器端脚本,你的 javascript 可以通过 AJAX 请求调用它来获取加密货币的价值。
服务器端脚本会 return 一个原始值,或者它会在数据库或其他存储系统中查找该值。
实现由您决定,但是一个简单的 php 文件可以 return 像这样:
<?php
setlocale(LC_MONETARY, 'en_US.UTF-8');//make sure we have USD dollar sign
$value = 3.0;//or get your value from where ever you need to
$output = money_format('%.2n', $value);
echo($output);
?>
现在 javascript 您可以向 php 脚本发出这样的请求:
function getcurrencyvalue() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("currency").innerHTML = this.responseText;
}
};
xhttp.open("GET", "currency.php", true);//the GET request should be where you put the php file.
xhttp.send();
}
以及所有将放入 html 文件(或您正在使用的文件)的所有内容:
<!DOCTYPE html>
<html>
<head>
<title>Currency Test</title>
<script src="currency.js"></script>
<script>
getcurrencyvalue();
</script>
</head>
<body>
<h1>Currency Value is:</h1>
<div id="currency">
</div>
</body>
</html>
完成所有设置后,您需要做的就是让您的客户加载该页面,对于多个用户,它始终是您 return 的值。