然而,当我尝试做到这一点时,它不起作用。我不确定硬件是否能得到正确的信息(还没有测试过),但我应该能够进入下一页。通过测试,如果 $。post(requestURL,{params:thisGame,access_token:accessToken}); 被注释掉,它会进入下一页。
请大家多多支持
<!DOCTYPE HTML> < html> < body> < input id =Memory Buttontype =buttonvalue =Memory Gameonclick =myFunction1('Memory')/> < script language =javascripttype =text / javascript> var deviceID =设备ID; var accessToken =访问令牌; var baseURL =api.particle.io/v1/devices/; var whichGame =setGame; 函数myFunction1(thisGame) { var requestURL =api.spark.io/v1/devices/+ deviceID +/+ whichGame + /?access_token =+ accessToken; $ .post(requestURL,{params:thisGame,access_token:accessToken}); myFunction2(); } 函数myFunction2() { window.location.href ='twin-cities.umn.edu/'; } < / script> < / body> < / html>解决方案
在头文件中包含jquery cdn:
< script src =ajax.googleapis/ajax/libs/jquery/3.1.1/jquery .min.js>< /脚本>您可以尝试使用以下代码的AJAX:
var ajax = $ .ajax({ url:yourURL, data:data, type:'POST', beforeSend:function(xhr){ xhr.setRequestHeader(token,token_value); //将标记附加到标题}, error:function(response){// if服务器出现问题console.log(response); alert(出错了,请稍后再试。); }, success:function(response) { //调用你想在这里调用的函数} });其中'data'是包含所有要发送的数据的字符串化对象。
So, I am trying to make a web page that, after the button for Memory game is clicked it will do two things:
However, when I try to make it do this, it doesn't work. I am not sure if the hardware is even getting the correct message (haven't tested it yet) but I should be able to go to the next page. Through testing, it goes to the next page if the $.post( requestURL, { params: thisGame, access_token: accessToken }); is commented out.
Please, any assistance is much appreciated
<!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 = "api.particle.io/v1/devices/"; var whichGame="setGame"; function myFunction1(thisGame) { var requestURL = "api.spark.io/v1/devices/" + deviceID + "/" + whichGame + "/?access_token=" + accessToken; $.post( requestURL, { params: thisGame, access_token: accessToken }); myFunction2(); } function myFunction2() { window.location.href = 'twin-cities.umn.edu/'; } </script> </body> </html>解决方案
Include the jquery cdn in the header like this:
<script src="ajax.googleapis/ajax/libs/jquery/3.1.1/jquery.min.js"></script>And you may try using AJAX with the following code:
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 } });Where 'data' is a stringified object containing all the data you want to send.
更多推荐
通过网页使用硬件
发布评论