gpt4 book ai didi

javascript - 使用从 Javascript 到 Ajax 的值

转载 作者:行者123 更新时间:2023-11-30 06:52:13 25 4
gpt4 key购买 nike

我正在尝试从 javascript (Virtualjoystick.js) 获取两个值(x 和 y)并将它们传递给 (prototype.js) Ajax 脚本。我一直在这里和其他网站上阅读类似的问题和答案,但我并没有真正“明白”!任何帮助将不胜感激。

这是我目前所拥有内容的简化版本。

<html>
<head>
<script src="JavaScripts/prototype.js"></script>
<script src="JavaScripts/virtualjoystick.js"></script>
</head>
<body>
<div id="info">
<span id="result"><b>Joystick:</b> X:0 Y:0</span>
</div>
<div id="container">
<canvas height="300" width="300"></canvas>
<canvas height="300" width="300"></canvas>

<script>
console.log("touchscreen is", VirtualJoystick.touchScreenAvailable() ? "available" : "not available");

var joystick = new VirtualJoystick({
container : document.getElementById('container'),
mouseSupport : true,
stationaryBase: true,
baseX: 150,
baseY: 150,
limitStickTravel: true,
stickRadius: 100

});
joystick.addEventListener('touchStart', function(){
console.log('down')
})
joystick.addEventListener('touchEnd', function(){
console.log('up')
})

setInterval(function(){
var outputEl = document.getElementById('result');
outputEl.innerHTML = '<b>Result:</b> '
+ ' X:'+parseInt(joystick.deltaX())
+ ' Y:'+parseInt(joystick.deltaY())
}, 1/30 * 1000);
</script>


</div>

<div>
<form>
<input type="button" value="On" onClick="go('60.30')" style="font-size:200%;"><br />
<input type="button" value="Off" onClick="go('0.0')" style="font-size:200%;">
</form>
<script type="text/javascript">
function go(qry) {
new Ajax.Request('motor_control.py?q='+ qry,
{method: 'GET'}
);
}
</script>
</div>


</body>
</html>
我需要的是 virtualjoystick.js 脚本中的 parseInt(joystick.deltaX())+parseInt(joystick.deltaY()) 被写入到Ajax.请求。类似于:

new Ajax.Request('motor_control.py?q='+parseInt(joystick.deltaX())
+ '.'+parseInt(joystick.deltaY()),
我试过了,但似乎没有用!

你可能会说 JavaScript 对我来说是一门外语,所以任何帮助都将不胜感激。

最佳答案

如果您想使用纯 JavaScript,您可以使用类似于下面示例的内容。您必须记住,当使用 AJAX 类型的函数时,您将只能对函数从回调函数或 promise 函数中检索到的数据执行操作。单击以下链接以查看 Ajax 功能的运行情况。

Live Example

JavaScript:

//path to the file you are sending and retrieving data
//this will differ from the Plunker link code because
//I do not have access to the url in your example.
var url = "motor_control.py?q='+ qry";

//call get Ajax function
get(url,function(data){//begin call back function


//do something with data variable here


//change the innerHTML of the example id element.
document.getElementById("example").innerHTML = "I was rendered by an AJAX function!"


});//end call back function

function get(url,fn){//begin ajax function

var contentType;

//variable to hold the xmlhttp object
var xmlhttp = null;


//if the browser contains the object
if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari

//create a new XMLHttpRequest object
xmlhttp=new XMLHttpRequest();
}


//add the event listenter
xmlhttp.onreadystatechange = function(){

//if the status of the request is good
if (xmlhttp.readyState===4 && xmlhttp.status===200){//begin if then

//get the response text and store it in the data variable
var data = xmlhttp.responseText;

//call the callback function
fn(data,fn);

}//end if then


};//end function


//open a the http request
xmlhttp.open("GET",url,true);

//set the request header
xmlhttp.setRequestHeader("Content-type",contentType);

//send the the request
xmlhttp.send();


}//end get function

关于javascript - 使用从 Javascript 到 Ajax 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36153505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com