function btn_onclick() //假设一个button点了以后触发这个ajax
{
createXMLHttpRequest();
var url="sample.ashxid=1&name=a"; //这里假设传给sample.ashx,传2个值,id=1和name=a
xmlHttp.open( "POST" ,url,true);
xmlHttp.onreadystatechange=Response; //回调函数是Response()
xmlHttp.send(null);
}
functionResponse()
{
if( xmlHttp.readyState==4 && xmlHttp.status==200 )
{
alert( xmlHttp.responseText ); //弹出一个框显示服务器返回的内容
}
}
/* $.post方式 */
function btn_onclick() //同样还是这个事件和函数,还是点了以后触发
{
/*
同样还是sample.ashx,同样是id=1&name=a
这里的function(msg)是回调的函数,你可以把执行的内容直接写在{}里,msg表示服务器返回的数据。
为了和上面的方式更好的对应起来,我这里依然让他调用Response,但是需要增加参数msg
*/
$.post("sample.ashx",{ id:"1",name:"a" }, function(msg){ Response(msg); });
}
function Response(msg)
{
alert( msg ); //弹出一个框显示服务器返回的内容
}
jquery+ajax+asp.net实现Ajax操作
2010-05-17 01:46:41阅读143 评论0 字号:大中小
文章简介:关于jquery+ajax+asp.net实现Ajax操作的简介
jquery,ajax,asp.net
是jquery+ajax+ashx的 现在这个是Handler.ashx:
========================================================================
<%@ WebHandlerLanguage="C#" %>
using System;
using System.Web;
...jquery+ajax+asp.net实现Ajax操作
是jquery+ajax+ashx的 现在这个是Handler.ashx:
========================================================================
<%@ WebHandlerLanguage="C#" %>
using System;
using System.Web;
public class Handler :IHttpHandler {
publicvoid ProcessRequest (HttpContext context) {
charmethod = Convert.ToChar(context.Request.Params["m"]);
context.Response.ContentType= "text/plain";
switch(method)
{
case'a':
context.Response.Write("HelloWorld<br/>This is a sample");
return;
case'b':
context.Response.Write("HelloWorld<br/>This is b sample");
return;
}
context.Response.Flush();
}
}
================================================================
jquery调用代码:
=================================================================
$(document).ready(function(){
$("#test2").click(function(){
$.ajax({
type: "post",
url: "Handler.ashx",
data: {m:'a'},
success: function(result){
$("#testText").append(result+ "<br/>");
}
});
});
});
$(document).ready(function(){
$("#test3").click(function(){
$.ajax({
type: "post",
文章简介:关于jquery+ajax+asp.net实现Ajax操作的简介
jquery,ajax,asp.net
是jquery+ajax+ashx的 现在这个是Handler.ashx:
========================================================================
<%@ WebHandlerLanguage="C#" %>
using System;
using System.Web;
...jquery+ajax+asp.net实现Ajax操作
是jquery+ajax+ashx的 现在这个是Handler.ashx:
========================================================================
<%@ WebHandlerLanguage="C#" %>
using System;
using System.Web;
public class Handler :IHttpHandler {
publicvoid ProcessRequest (HttpContext context) {
charmethod = Convert.ToChar(context.Request.Params["m"]);
context.Response.ContentType= "text/plain";
switch(method)
{
case'a':
context.Response.Write("HelloWorld<br/>This is a sample");
return;
case'b':
context.Response.Write("HelloWorld<br/>This is b sample");
热门源码