简体   繁体   English

有人可以向我解释这段JavaScript / Ajax代码的作用是什么?

[英]Can someone explain to me what this JavaScript/Ajax part of code does?

I'm learning Ajax and I'm going through this example . 我正在学习Ajax,并且正在研究这个示例 What does this do? 这是做什么的? I don't understand the syntax variable = function(){ how is a function being assigned to a variable? 我不了解语法variable = function(){如何将函数分配给变量?

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

When readyState changes, if request finished and response is ready ( readyState==4 ) and document loaded correctly (HTTP Status Code 200 = OK!), append response text to the element with id #txtHint . 当readyState更改时,如果请求完成并且响应准备就绪( readyState==4 )并且文档已正确加载(HTTP状态码200 = OK!),则将响应文本附加到ID为#txtHint的元素上。


onreadystatechange stores a function (or the name of a function) to be called automatically each time the readyState property changes. onreadystatechange存储一个函数(或函数名),每次readyState属性更改时将自动调用该函数。

readyState holds the status of the XMLHttpRequest. readyState保留XMLHttpRequest的状态。 Changes from 0 to 4: 从0变为4:

  • 0: request not initialized 0:请求未初始化
  • 1: server connection established 1:建立服务器连接
  • 2: request received 2:收到请求
  • 3: processing request 3:处理要求
  • 4: request finished and response is ready 4:请求已完成且响应已准备就绪

status takes HTTP response codes: status采用HTTP响应代码:

  • 200: "OK" 200:“确定”
  • 404: Page not found 404页面不存在

onreadystatechange is a callback. onreadystatechange是一个回调。 It gets triggered when a particular event happens. 当特定事件发生时,它将被触发。 onreadystate is happens when the requests ready state changes. 当请求就绪状态更改时,发生onreadystate

In short onreadystate 简而言之,准备onreadystate

Stores a function (or the reference of a function) to be called automatically each time the readyState property changes

Now for the line xmlhttp.readyState==4 && xmlhttp.status==200 现在对于xmlhttp.readyState==4 && xmlhttp.status==200

readyState : Holds the status of the XMLHttpRequest.

 Changes from 0 to 4: 
0: request not initialized 
1: server connection established
2: request received 
3: processing request 
4: request finished and response is ready

And status Holds status status保持状态

200: "OK"
404: Page not found

So xmlhttp.readyState==4 && xmlhttp.status==200 condition is true when the response is ready and there is no issues 因此,当响应准备就绪且没有问题时, xmlhttp.readyState==4 && xmlhttp.status==200条件为true

xmlhttp.responseText contains the response sent from the server. xmlhttp.responseText包含从服务器发送的响应。

So document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 因此document.getElementById("txtHint").innerHTML=xmlhttp.responseText; changes the HTML of the element with id txtHint to the response that was received. 将ID为txtHint的元素的HTML更改为收到的响应。

Hope all the above made sense!!! 希望以上所有都有意义!!!

I know everyone is saying it's a callback specifically but the question you asked could better be answered by comparing what you're puzzled over with some more familiar code. 我知道每个人都专门说这是一个回调,但是您可以通过将您困惑的内容与一些更熟悉的代码进行比较来更好地回答您所提出的问题。

function myFunction()
{
    ...
}

So we know that calling myFunction() will run that code. 因此,我们知道调用myFunction()将运行该代码。

In Javascript, you can declare a function in a number of ways. 在Javascript中,您可以通过多种方式声明函数。 This means that this: 这意味着:

var myFunction = function()
{
    ...
}

Does exactly the same as the first example above. 与上面的第一个示例完全相同。 It creates a function that you can call using myFunction() . 它创建一个可以使用myFunction()调用的函数。

Add the callback into the mix in your question and we can see that 将回调添加到您的问题中,我们可以看到

xmlhttp.onreadystatechange = function()
{
    ...
}

is nothing more than assigning a function and containing code to the onreadystatechange property of object xmlhttp. 仅仅是为对象xmlhttp的onreadystatechange属性分配一个函数并包含代码而已。 Meaning that your code within the function will be called every time there is a state change in the xmlhttp object. 这意味着,每次xmlhttp对象中的状态更改时,都会调用该函数中的代码。

I'd like to address this comment: I've never been able to understand callbacks. 我想解决这个问题: I've never been able to understand callbacks.

Consider this analogy: 考虑这个比喻:

You need want to rent that movie that just came out on VHS, so you call Blockbuster and ask the attendant if they have a copy. 您需要租用刚在VHS上放映的电影,因此您打电话给Blockbuster,并询问服务员是否有副本。 Sadly though, they are super busy dealing with thousands of David Bowie fans who are trying to rent "Labyrinth" all at the same time and he does't have time to look up the information for you. 但是,令人遗憾的是,他们正忙于与成千上万名试图同时租用“迷宫”的David Bowie歌迷打交道,而他却没有时间为您查找信息。 So instead, he asks for your phone number. 因此,他要求您提供电话号码。 At some point in the future when the hordes of people have left and he has the time, he looks up the info you need, and calls you back on the number you provided. 在未来的某个时刻,当成群的人们离开并且他有时间时,他会查找您需要的信息,并通过您提供的电话号码回叫您。 Turns out the movie is sold out though, so he suggests "Dark Crystal" instead. 原来电影虽然卖完了,所以他建议改用《黑暗水晶》。

In your case you are dealing with an entity that is going to take a long time to do it's work as it needs to talk to remote servers, so it essentially asks you for your phone number so that when it's done you are called back as it were, with the requested data. 在您的情况下,您正在处理的实体需要花费很长时间才能完成工作,因为它需要与远程服务器进行通信,因此它实质上会询问您的电话号码,以便在完成操作后将您回叫是,具有请求的数据。

Does it make more sense now? 现在更有意义吗?

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

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