我有一个WCF操作,它在光盘上打开一个html文件并将其发送回客户端(浏览器)。该方法如下所示:
public Stream Home()
{
Console.WriteLine("Page requested");
// Open the stream and read it back.
using (FileStream fs = File.Open(@"index.html", FileMode.Open))
{
string page = "";
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
page += temp.GetString(b);
}
byte[] result = Encoding.UTF8.GetBytes(page);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
Console.Write(result);
return new MemoryStream(result);
}
}
这适用于vanilla JS XMLHttpRequest,但是当我添加jQuery CDN引用时,HTML似乎被加载了两次。
为什么会这样?
index.html看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<script type="text/javascript">
// setTimeout(function() {
// var request = new XMLHttpRequest();
// if ('withCredentials' in request) {
// request.onreadystatechange = function() {
// if (request.readyState === 4) {
// if (request.status === 200) {
// document.getElementById('ajaxContent').innerHTML = request.responseText;
// } else {
// alert('An error has occurred');
// }
// }
// };
// request.open('GET', 'http://localhost:xx/WebService/JsTest', true);
// request.send(null);
// }
// }, 2000)
$(document).ready(function() {
alert('hi');
})
</script>
</head>
<body>
<h1>Hello World</h1>
<div id="ajaxContent"></div>
</body>
</html>
结果页面(我的javascript已注释掉)如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<script type="text/javascript">
// setTimeout(function() {
// var request = new XMLHttpRequest();
// if ('withCredentials' in request) {
// request.onreadystatechange = function() {
// if (request.readyState === 4) {
// if (request.status === 200) {
// document.getElementById('ajaxContent').innerHTML = request.responseText;
// } else {
// alert('An error has occurred');
// }
// }
// };
// request.open('GET', 'http://localhost:xx/WebService/JsTest', true);
// request.send(null);
// }
// }, 2000)
$(document).ready(function() {
alert('hi');
})
</script>
</head>
<body>
<h1>Hello World</h1>
<div id="ajaxContent"></div>
</body>
</html>
UStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<script type="text/javascript">
// setTimeout(function() {
// var request = new XMLHttpRequest();
// if ('withCredentials' in request) {
// request.onreadystatechange = function() {
// if (request.readyState === 4) {
// if (request.status === 200) {
// document.getElementById('ajaxContent').innerHTML = request.responseText;
// } else {
// alert('An error has occurred');
// }
// }
// };
// request.open('GET', 'http://localhost:xx/WebService/JsTest', true);
// request.send(null);
// }
// }, 2000)
$(document).ready(function() {