問題描述
我是 XMLHttpRequest 的新手.我不明白為什么我們要在 send() 函數(shù)之前編寫 onload() 函數(shù).onload() 函數(shù)處理我們收到的響應,然后 send() 函數(shù)向服務器發(fā)送請求.所以根據(jù)我的理解,onload() 必須在 send() 函數(shù)之后編寫.有人可以幫助理解這一點.
I am new to XMLHttpRequest. I dont understand why do we write onload() function before send() function. onload() function process the response what we receive and send() function sends a request to server. So onload() has to be written after send() function as per my understanding. Can somebody help to understand this.
var xmlhttp = new XMLHttpRequest(),
method = 'GET',
url = 'https://developer.mozilla.org/';
xmlhttp.open(method, url, true);
xmlhttp.onload = function () {
// Do something with the retrieved data
};
xmlhttp.send();
推薦答案
我不明白為什么我們要在
send()
函數(shù)之前寫onload()
函數(shù).
I dont understand why do we write
onload()
function beforesend()
function.
以便在發(fā)送請求之前加載處理程序,因為發(fā)送請求將導致調(diào)用處理程序(如果成功).
So that the load handler is in place before the request is sent, since sending the request will result in calling the handler (if successful).
onload()
函數(shù)處理我們收到的響應,send()
函數(shù)向服務器發(fā)送請求.所以根據(jù)我的理解,onload()
必須寫在 send()
函數(shù)之后.
onload()
function process the response what we receive andsend()
function sends a request to server. Soonload()
has to be written aftersend()
function as per my understanding.
它在 send
被調(diào)用(由 XHR 基礎架構)(或可能在其期間)之后被調(diào)用.當您將它分配給 onload
時,您并不是在 調(diào)用 它.您只是定義它,以便當 XHR 需要調(diào)用它時它就在那里.
It's called after send
is called (by the XHR infrastructure) (or potentially during). When you assign it to onload
, you're not calling it. You're just defining it so that it's there when XHR needs to call it.
會發(fā)生什么:
- 您創(chuàng)建 XHR.
- 您為其
load
事件注冊一個處理程序(在您的情況下,通過將函數(shù)分配給onload
). - 你調(diào)用
send
.
- You create the XHR.
- You register a handler for its
load
event (in your case, by assigning a function toonload
). - You call
send
.
- 瀏覽器啟動(并可能完成)請求
load
事件.這會查找任何當前注冊的 load
處理程序,并將對這些處理程序的調(diào)用(如果有)排隊.這些調(diào)用會在主 JavaScript 線程可用于運行它們時立即運行.
load
event. That looks for any currently-registered handlers for load
and queues calls to those handlers, if any. Those calls are run as soon as the main JavaScript thread is available to run them.很多時候,你會以錯誤的方式逃脫懲罰,因為在請求完成時,你已經(jīng)將 load
處理程序放在那里;但不是總是.load
是一個事件.如果可以立即滿足請求(例如,從緩存中),瀏覽器可以在 send
期間觸發(fā) load
,并查看是否有任何 load
處理程序在 調(diào)用 send
期間,如果沒有,則不對任何回調(diào)的調(diào)用進行排隊.稍后當您附加處理程序時,該事件已經(jīng)被觸發(fā)(當沒有附加處理程序時).
Very often, you'd get away with doing it the wrong way around because by the time the request completes, you'll have put the load
handler there; but not always. load
is an event. If the request can be satisfied immediately (for instance, from cache), the browser could fire load
during send
, and look to see if there's any load
handler during the call to send
, and if there isn't, not queue a call to any callback. Later when you attach a handler, the event has already been fired (when none were attached).
所以你必須在調(diào)用 send
之前附加處理程序.
So you have to attach the handler before calling send
.
這篇關于為什么我們在 XMLHttpRequest 中寫 send() 之前要寫 onload() 函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!