Skip to content

Instantly share code, notes, and snippets.

@wjzhangq
Created November 23, 2010 02:55
Show Gist options
  • Save wjzhangq/711152 to your computer and use it in GitHub Desktop.
Save wjzhangq/711152 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>cross 跨域</title>
</head>
<body>
<input type='button' value='开始测试' onclick='crossDomainRequest()' />
<div id="content"></div>
<script type="text/javascript"><!--
var xhr = new XMLHttpRequest();
var url = 'http://local.web.com/cross/api.php';
function crossDomainRequest() {
document.getElementById("content").innerHTML = "开始……";
if (xhr) {
xhr.open('GET', url, true);
xhr.onreadystatechange = handler;
xhr.send();
} else {
document.getElementById("content").innerHTML = "不能创建 XMLHttpRequest";
}
}
function handler(evtXHR) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var response = xhr.responseText;
document.getElementById("content").innerHTML = "结果:" + response;
} else {
document.getElementById("content").innerHTML = "不允许跨域请求。该功能需要在Internet Explorer 8+, Firefox 3.5+, Safari 4+和 Chrome才能正确运行";
}
}
else {
document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
}
}
// --></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment