1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
---
title: XMLHttpRequest.onreadystatechange
slug: Web/API/XMLHttpRequest/readystatechange_event
tags:
- XHR
- XMLHttpRequest
- 参考
- 处理程序
- 属性
- 接口
translation_of: Web/API/XMLHttpRequest/onreadystatechange
original_slug: Web/API/XMLHttpRequest/onreadystatechange
---
<div>{{APIRef}}</div>
<p>只要 <code>readyState</code> 属性发生变化,就会调用相应的<a href="/zh-CN/docs/Web/API/EventHandler">处理函数</a><font face="consolas, Liberation Mono, courier, monospace">。</font>这个回调函数会被用户线程所调用。<strong><code>XMLHttpRequest.onreadystatechange</code></strong> 会在 {{domxref("XMLHttpRequest")}} 的{{domxref("XMLHttpRequest.readyState", "readyState")}} 属性发生改变时触发 {{event("readystatechange")}} 事件的时候被调用。</p>
<div class="warning">
<p><strong>警告</strong>:这个方法不该用于同步的requests对象,并且不能在内部(C++)代码中使用.</p>
</div>
<p>当一个 <code>XMLHttpRequest</code> 请求被 <a href="/en-US/docs/Web/API/XMLHttpRequest/abort">abort()</a> 方法取消时,其对应的 <code>readystatechange</code> 事件不会被触发。</p>
<div class="note">
<p>UPDATE: 在下面的浏览器版本中会触发 (Firefox 51.0.1, Opera 43.0.2442.991, Safari 10.0.3 (12602.4.8), Chrome 54.0.2840.71, Edge, IE11). 例子在 <a href="https://jsfiddle.net/merksam/ve5oc0gn/">here</a> - 重新加载几次页面即可。</p>
</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><em>XMLHttpRequest</em>.onreadystatechange = <em>callback</em>;</pre>
<h3 id="取值">取值</h3>
<ul>
<li>当 <code>readyState</code> 的值改变的时候,<code><em>callback</em></code> 函数会被调用。</li>
</ul>
<h2 id="Example" name="Example">示例</h2>
<pre class="brush: js">var xhr= new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(<em>method</em>, <em>url</em>, true);
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr.responseText)
}
}
xhr.send();</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">注释</th>
</tr>
<tr>
<td>{{SpecName('XMLHttpRequest', '#handler-xhr-onreadystatechange')}}</td>
<td>{{Spec2('XMLHttpRequest')}}</td>
<td>WHATWG living standard</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(1)}}</td>
<td>{{CompatGeckoDesktop(1.0)}}</td>
<td>{{CompatIe(7)}}<sup>[1]</sup></td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatSafari(1.2)}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>1.0</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] IE 5 和 6可以通过使用 <code>ActiveXObject()</code> 支持ajax。</p>
|