1、定义页面click按钮,通过此按钮触发ajax异步取后台数据功能
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h2>Let AJAX change this text</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
</body>
</html>
2、定义函数loadDoc来执行ajax与服务器交互的动作:
function loadDoc() {
//定意思XMLHttpRequest对象
var xhttp = new XMLHttpRequest()
//定义返回状态为成功时的返回结果显示
xhttp.onreadystatechange = function() {
//返回值状态为4或者响应码为200是成功
if (this.readyState == 4 &&this.status == 200) {
//给标签div赋值返回结果responseText
document.getElementById("demo").innerHTML = this.responseText
}
}
//开始执行后台取数据
xhttp.open("GET", "ajax_info.txt", true)
//开始发送请求
xhttp.send()
}
代码如下:后台方法:
protected string CsharpVoid(string strCC)
{
strCC = "你好!" + strCC
return strCC
}
前台JS
function Init()
{
var v = "中国"
var s = '<%=CsharpVoid("'+v+'") %>'
alert(s)
}