通过网页使用硬件 [英] Using hardware through webpage

查看:172
本文介绍了通过网页使用硬件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图制作一个网页,点击Memory按钮后,它会做两件事:


  1. 它将设置我在硬件代码中写入的云功能,并相应地执行操作,并且
  2. 它将完全转到另一个网页。

然而,当我尝试做到这一点时,它不起作用。我不确定硬件是否能得到正确的信息(还没有测试过),但我应该能够进入下一页。通过测试,如果 $。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 =https://api.particle.io/v1/devices/;
var whichGame =setGame;

函数myFunction1(thisGame)
{
var requestURL =https://api.spark.io/v1/devices/+ deviceID +/+ whichGame + /?access_token =+ accessToken;

$ .post(requestURL,{params:thisGame,access_token:accessToken});

myFunction2();
}

函数myFunction2()
{
window.location.href ='http://twin-cities.umn.edu/';
}

< / script>
< / body>
< / html>


解决方案

在头文件中包含jquery cdn:

 < script src =https://ajax.googleapis.com/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:

  1. It will set the cloud function I wrote in the hardware's code and act accordingly and
  2. 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 = "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>

解决方案

Include the jquery cdn in the header like this:

<script src="https://ajax.googleapis.com/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.

这篇关于通过网页使用硬件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆