标签:process 访问 存在 声明 mozilla request对象 事件处理 false content
httpRequest.onreadystatechange = nameOfTheFunction;
httpRequest.onreadystatechange = function(){
// Process the server response here.
};
httpRequest.open(‘GET‘, ‘http://www.example.org/some.file‘, true);
httpRequest.send();
"name=value&anothername="+encodeURIComponent(myVar)+"&so=on"
httpRequest.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘);
httpRequest.onreadystatechange = nameOfTheFunction;
if (httpRequest.readyState === XMLHttpRequest.DONE) {
// Everything is good, the response was received.
} else {
// Not ready yet.
}
Value | State | Description |
0 | UNSENT | Client has been created. open() not called yet. |
1 | OPENED | open() has been called. |
2 | HEADERS_RECEIVED | send() has been called, and headers and status are available. |
3 | LOADING | Downloading; responseText holds partial data. |
4 | DONE | The operation is complete. |
if (httpRequest.status === 200) {
// Perfect!
} else {
// There was a problem with the request.
// For example, the response may have a 404 (Not Found)
// or 500 (Internal Server Error) response code.
}
<button id="ajaxButton" type="button">Make a request</button>
<script>
(function() {
var httpRequest;
document.getElementById("ajaxButton").addEventListener(‘click‘, makeRequest);
function makeRequest() {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert(‘Giving up :( Cannot create an XMLHTTP instance‘);
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open(‘GET‘, ‘test.html‘);
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert(‘There was a problem with the request.‘);
}
}
}
})();
</script>
function alertContents() {
try {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert(‘There was a problem with the request.‘);
}
}
}
catch( e ) {
alert(‘Caught Exception: ‘ + e.description);
}
}
<?xml version="1.0" ?>
<root>
I‘m a test.
</root>
...
onclick="makeRequest(‘test.xml‘)">
...
var xmldoc = httpRequest.responseXML;
var root_node = xmldoc.getElementsByTagName(‘root‘).item(0);
alert(root_node.firstChild.data);
<label>Your name:
<input type="text" id="ajaxTextbox" />
</label>
<span id="ajaxButton" style="cursor: pointer; text-decoration: underline">
Make a request
</span>
document.getElementById("ajaxButton").onclick = function() {
var userName = document.getElementById("ajaxTextbox").value;
makeRequest(‘test.php‘,userName);
};
function makeRequest(url, userName) {
...
httpRequest.onreadystatechange = alertContents;
httpRequest.open(‘POST‘, url);
httpRequest.setRequestHeader(‘Content-Type‘, ‘application/x-www-form-urlencoded‘);
httpRequest.send(‘userName=‘ + encodeURIComponent(userName));
}
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse(httpRequest.responseText);
alert(response.computedString);
} else {
alert(‘There was a problem with the request.‘);
}
}
}
$name = (isset($_POST[‘userName‘])) ? $_POST[‘userName‘] : ‘no name‘;
$computedString = "Hi, " . $name;
$array = [‘userName‘ => $name, ‘computedString‘ => $computedString];
echo json_encode($array);
ajax(Asynchronous JavaScript + XML) 技术学习
标签:process 访问 存在 声明 mozilla request对象 事件处理 false content
原文地址:http://www.cnblogs.com/qiqizuo/p/6959197.html