通过网页使用硬件

编程入门 行业动态 更新时间:2024-10-24 12:34:19
本文介绍了通过网页使用硬件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 因此,我试图制作一个网页,点击Memory按钮后,它会做两件事:

  • 它将设置我在硬件代码中写入的云功能,并相应地执行操作,并且
  • 它将完全转到另一个网页。
  • 然而,当我尝试做到这一点时,它不起作用。我不确定硬件是否能得到正确的信息(还没有测试过),但我应该能够进入下一页。通过测试,如果 $。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:

  • It will set the cloud function I wrote in the hardware's code and act accordingly and
  • It will go to another web page entirely.
  • 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.

    更多推荐

    通过网页使用硬件

    本文发布于:2023-10-26 12:02:48,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:硬件   网页

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!