通过网页使用硬件

时间:2016-12-05 16:20:21

标签: javascript jquery html

所以,我正在尝试创建一个网页,在点击Memory游戏按钮后,它会做两件事:

  1. 它将设置我在硬件代码中编写的云功能并采取相应的行动
  2. 它将完全转到另一个网页。
  3. 但是,当我尝试这样做时,它不起作用。我不确定硬件是否正在获取正确的消息(尚未测试)但我应该能够转到下一页。通过测试,如果注释掉$.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>
    

2 个答案:

答案 0 :(得分:2)

在标题中包含jquery cdn,如下所示:

<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
        }
    });

数据&#39;是一个包含所有要发送的数据的字符串化对象。

答案 1 :(得分:0)

变化:

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>