下面是子页面的代码:
<!DOCTYPE html>
<html>
<head>
<title>10.2.2</title>
<meta charset="utf-8" />
<script type="text/javascript">
function sendMessage(){
var msg = document.getElementById("sendInfo").value;
top.postMessage(msg,"http://localhost:8080");
}
</script>
</head>
<body>
<input type="text" id="sendInfo" />
<input type="button" name="btnSend" id="btnSend" value="send" />
</body>
</html>
XMLHttpRequest Level 2
跨源XMLHttpRequest版本
首先创建一个XMLHttpRequest的实例,然后向远程主机发送一个http请求:
var xhr = new XMLHttpRequest( );
xhr.open("POST", "http://localhost:8081");
xhr.send( );
xhr.timeout = 3000; 设置时限为3000毫秒,即3秒
xhr.ontimeout = function(event){
alert("请求超时");
}
FormData对象:通过JavaScript用键值对来模拟一系列表单控件。FormData的append( )方法可以向该对象中添加各种类型的数据。
上传文件:
<form enctype="multipart/form-data" method="post" name="fileinfo">
<input type="file" name="file" required />
</form>
<div id="msg"></div>
完整示例代码(略)
UploadFile.html