简体   繁体   English

有人能解释一下这个 ajax 代码的作用吗?

[英]Can someone explain what this ajax code does?

Can someone explain what this ajax code does?有人能解释一下这个 ajax 代码的作用吗?

function ajaxProgress(){
 //Math.random() is for bitchy ie to prevent caching the xml.
 $.get('sample.ff?do=progressInfo&type=sampletype&dummy='+Math.random(), { dataType:   'xml'},      function(xml) {
 //if the import is running get infos about the progress...
 if($('/importProgress/running', xml).text() == 'true') {
 //..there are no infos yet, so it was just started..
 if($('/importProgress/progress', xml) == null || $('/importProgress/progress', xml).text() == ''){
 //do something
 }
 ..........
 setTimeout( "ajaxProgress()", 1000);

This function is calling itself recursively every second.这个 function 每秒都在递归调用自己。 It sends an AJAX GET request to Import.ff and passing 3 query string parameters: do=progressInfo , type=sampletype and a random number.它向Import.ff发送一个 AJAX GET 请求并传递 3 个查询字符串参数: do=progressInfotype=sampletype和一个随机数。 This random number is appended to the url because GET requests are cached by browsers and by this it ensures that it gets fresh content from the server on each request.这个随机数被附加到 url 中,因为 GET 请求被浏览器缓存,这样可以确保它在每次请求时从服务器获取最新内容。

The server itself sends an XML file as response.服务器本身发送一个 XML 文件作为响应。 This XML file contains some nodes like:此 XML 文件包含一些节点,例如:

<importProgress>
    <running>true</running>
    <progress>20</progress>
</importProgress>

So the script parses this XML in the success callback of the AJAX request.所以脚本在AJAX请求的成功回调中解析了这个XML。 It tries to get the values of the running and progress nodes.它试图获取runningprogress节点的值。 If running=true then it checks whether there's a progress node and does some processing with it.如果running=true则检查是否有进度节点并对其进行一些处理。 Finally it calls itself 1 second later using the setTimeout function.最后它在 1 秒后使用setTimeout function 调用自己。 And so on.等等。

So basically this script reports progress from some server operation by polling the server at 1 seconds intervals using AJAX GET requests and parsing the response.所以基本上这个脚本通过使用 AJAX GET 请求并解析响应以 1 秒的间隔轮询服务器来报告某些服务器操作的进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM