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;
}
})
});
}
默认.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>
默认.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 文件的完整代码