【发布时间】:2012-05-28 20:01:40
【问题描述】:
JScript.js 文件
function Helloworld() {
$(document).ready(function () {
$.ajax
({
type: "POST",
url: "Default.aspx/Helloworld",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
document.getElementById('textbox').value = msg.d;
}
})
});
}
Default.aspx
<head runat="server">
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
//Works Fine when I uncomment this
<%-- <script src="JScript.js" type="text/javascript"></script>--%>
<script type="text/javascript" language="javascript">
(function () {
var load = document.createElement('script');
load.type = 'text/javascript';
load.src = 'JScript.js';
load.async = true;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body') [0]).appendChild(load);
})();
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="input" id="textbox" />
</form>
</body>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "KeyHelloworld", "<script type='text/javascript'>Helloworld()</script>");
}
[WebMethod(EnableSession = true)]
public static string Helloworld()
{
return "Hello World";
}
我正在尝试将此 JavaScript 文件异步加载到页面中,但上面的函数未执行是异步加载 JavaScript 文件的完整代码
【问题讨论】:
-
控制台没有报错?
-
不,我没有收到任何错误。当我调用 JavaScript 文件 async 时,我只是看不到输出。但是当我取消注释时我看到了输出。 --%>
标签: javascript asp.net ajax