[英]Using hardware through webpage
因此,我尝试制作一个网页,在单击“内存”游戏的按钮后,它将做两件事:
但是,当我尝试使其执行此操作时,它将不起作用。 我不确定硬件是否能收到正确的消息(尚未对其进行测试),但是我应该可以转到下一页。 通过测试,如果$.post( requestURL, { params: thisGame, access_token: accessToken });
已被注释掉。
请提供任何协助
<!DOCTYPE html>
<html>
<body>
<input id="Memory Button" type="button" value="Memory Game" onclick="myFunction1('Memory')"/>
<script language="javascript" type="text/javascript">
var deviceID = "device id";
var accessToken = "access token";
var baseURL = "https://api.particle.io/v1/devices/";
var whichGame="setGame";
function myFunction1(thisGame)
{
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + whichGame + "/?access_token=" + accessToken;
$.post( requestURL, { params: thisGame, access_token: accessToken });
myFunction2();
}
function myFunction2()
{
window.location.href = 'http://twin-cities.umn.edu/';
}
</script>
</body>
</html>
像这样在标题中包含jcdn:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
您可以尝试将AJAX与以下代码结合使用:
var ajax = $.ajax({
url: yourURL,
data: data,
type: 'POST',
beforeSend : function(xhr) {
xhr.setRequestHeader("token", token_value); //append token to header
},
error : function(response){ //if something went wrong on server
console.log(response);
alert("Something went wrong. Please try later.");
},
success: function(response){
//call the function you wanted to call here
}
});
其中“数据”是包含您要发送的所有数据的字符串化对象。
更改:
function myFunction1(thisGame)
{
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + whichGame + "/?access_token=" + accessToken;
$.post( requestURL, { params: thisGame, access_token: accessToken });
myFunction2();
}
至:
function myFunction1(thisGame)
{
var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + whichGame + "/?access_token=" + accessToken;
$.post( requestURL, { params: thisGame, access_token: accessToken })
.done(myFunction2)
.fail(function(){ alert('FAIL!'); });
}
并将其添加到<html>
标记之后:
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
</head>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.