当前位置: 代码迷 >> ASP.NET >> javascript中怎么访问后台方法
  详细解决方案

javascript中怎么访问后台方法

热度:8842   发布时间:2013-02-26 00:00:00.0
javascript中如何访问后台方法
如题!谢谢

------解决方案--------------------------------------------------------
<span onclick= 'javascript:alert( " <%= behindFunction() %> "); '> </span>

...

或者用AJAX实现。
------解决方案--------------------------------------------------------
<%=XXXXX%>
------解决方案--------------------------------------------------------
1.创建新的 XMLHttpRequest 对象
<script language= "javascript " type= "text/javascript ">
var xmlHttp = new XMLHttpRequest();
</script>


2.用 JavaScript 代码捕获和设置字段值
// Get the value of the "phone " field and stuff it in a variable called phone
var phone = document.getElementById( "phone ").value;
// Set some values on a form using an array called response

document.getElementById( "order ").value = response[0];
document.getElementById( "address ").value = response[1];

3.在 Microsoft 浏览器上创建 XMLHttpRequest 对象
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP ");
} catch (e) {
try {
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP ");
} catch (e2) {
xmlHttp = false;
}
}

4.以支持多种浏览器的方式创建 XMLHttpRequest 对象
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version > = 5)
try {
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP ");
} catch (e) {
try {
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP ");
} catch (e2) {
xmlHttp = false;
}
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined ') {
xmlHttp = new XMLHttpRequest();
}

5.发出 Ajax 请求
function callServer() {
// Get the city and state from the web form
var city = document.getElementById( "city ").value;
var state = document.getElementById( "state ").value;
// Only go on if there are values for both fields
if ((city == null) || (city == " ")) return;
if ((state == null) || (state == " ")) return;
// Build the URL to connect to
var url = "/scripts/getZipCode.php?city= " + escape(city) + "&state= " + escape(state);
// Open a connection to the server
xmlHttp.open( "GET ", url, true);
// Setup a function for the server to run when it 's done
xmlHttp.onreadystatechange = updatePage;
// Send the request
xmlHttp.send(null);
}

6. 处理服务器响应
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById( "zipCode ").value = response;
}
}

7. 启动一个 Ajax 过程
<form>
<p> City: <input type= "text " name= "city " id= "city " size= "25 "
onChange= "callServer(); " /> </p>
<p> State: <input type= "text " name= "state " id= "state " size= "25 "
onChange= "callServer(); " /> </p>
<p> Zip Code: <input type= "text " name= "zipCode " id= "city " size= "5 " /> </p>
</form>


  相关解决方案