使用Javascript到Ajax的值

使用Javascript到Ajax的值,javascript,javascript-objects,Javascript,Javascript Objects,我试图从一个javascript(virtualloggage.js)中获取两个值(x和y),并将它们传递给一个(prototype.js)Ajax脚本。我已经在这里和其他网站上阅读了类似的问题和答案,但我并没有真正“理解”!任何帮助都将不胜感激 这是我到目前为止所做的一个简化版本 操纵杆:X:0 Y:0 console.log(“触摸屏是”,虚拟操纵杆.touchScreenAvailable()?“可用”:“不可用”); var操纵杆=新的虚拟操纵杆({ 容器:document.getE

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

这是我到目前为止所做的一个简化版本


操纵杆:X:0 Y:0
console.log(“触摸屏是”,虚拟操纵杆.touchScreenAvailable()?“可用”:“不可用”);
var操纵杆=新的虚拟操纵杆({
容器:document.getElementById('container'),
鼠标支持:没错,
stationaryBase:没错,
baseX:150,
贝西:150,
limitStickTravel:没错,
斯蒂克拉迪乌斯:100
});
操纵杆。addEventListener('touchStart',函数(){
console.log('down')
})
操纵杆。addEventListener('touchEnd',函数(){
console.log('up')
})
setInterval(函数(){
var outputEl=document.getElementById('result');
outputEl.innerHTML='结果:'
+'X:'+parseInt(操纵杆.deltaX())
+“Y:”+parseInt(操纵杆.deltaY())
}, 1/30 * 1000);

功能go(qry){ 新的Ajax.Request('motor_control.py?q='+qry, {method:'GET'} ); }
如果您想使用纯JavaScript,可以使用下面的示例。您必须记住,当使用AJAX类型的函数时,您只能在回调函数或promise函数中对该函数检索到的数据执行操作。单击下面的链接以查看Ajax函数的运行情况

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,可以使用下面的示例。您必须记住,当使用AJAX类型的函数时,您只能在回调函数或promise函数中对该函数检索到的数据执行操作。单击下面的链接以查看Ajax函数的运行情况

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

“它似乎不起作用”-你期望发生什么?到底发生了什么?浏览器的开发人员工具控制台中是否报告了任何错误?您是否在开发人员工具的“网络”选项卡中看到HTTP请求?请求是否按照您的预期构造?它得到回应了吗?响应上的状态代码是否正确?回答的内容正确吗?什么都没有发生!日志中什么也没有出现。我的假设是我的“尝试”失败了!老实说,我没有想到开发人员工具控制台。我的经验都是PHP和Python,所以日志文件就是我开始调试的地方!我来看看开发人员控制台会说什么。“它似乎不起作用”-您期望发生什么?到底发生了什么?浏览器的开发人员工具控制台中是否报告了任何错误?您是否在开发人员工具的“网络”选项卡中看到HTTP请求?请求是否按照您的预期构造?它得到回应了吗?响应上的状态代码是否正确?回答的内容正确吗?什么都没有发生!日志中什么也没有出现。我的假设是我的“尝试”失败了!老实说,我没有想到开发人员工具控制台。我的经验都是PHP和Python,所以日志文件就是我开始调试的地方!我来看看开发者控制台要说什么。你似乎完全忽略了问题的背景,而倾向于给出一个通用的Ajax入门教程。为什么你要在GET请求上设置
内容类型
请求头?为什么你在2016年支持IE 6?为什么要使用
参数[1]
而不是
fn
?移动
fn(数据)的意义是什么编码为自己的函数?这只会让代码膨胀。你似乎完全忽略了问题的背景,而倾向于提供一个通用的Ajax入门教程。为什么你要在GET请求上设置
内容类型
请求头?为什么你在2016年支持IE 6?为什么要使用
参数[1]
而不是
fn
?移动
fn(数据)的意义是什么编码为自己的函数?那只会使代码膨胀。