jianmingyuan

jquery ajax

 

例子:    function JudgeUserName()
        {
            $.ajax({
            type:"GET",
            http://www.cnblogs.com/Ilin631/admin/%22Default2.aspx",
            dataType:"html",
            data:"userName="+$("#txtName").val(),
            beforeSend:function(XMLHttpRequest)
                {
                    $("#showResult").text("正在查询...");
                  // Pause(this,100000);
                },
            success:function(msg)
                {  
                    $("#showResult").html(msg);
                    $("#showResult").css("color","red");
                },
           complete:function(XMLHttpRequest,textStatus)
                {
                    //隐藏正在查询图片
                },
          error:function()
               {
                    //错误处理
               }
            });
        }

其中error是必须的,不能不写。

datatype:xml script html json

html 的后台返回的放在response.write()里:

string userName = Request.QueryString["userName"].ToString();
        if (userName == "James Hao")
        {
            Response.Write("用户名已经存在!");
        }
        else
        {
            Response.Write("您可以使用此用户名!");
        }

 

json: 后台放在:datatype:"json";

        string name = Context.Request["userName"];
        if (name == "aa")
        {
            Context.Response.Write("1");
        }
        else
        {
            Context.Response.Write("0");
        }
       

 

 

 

 

 

 

 

 

 

 

 

1判断浏览器支持。

<script type="text/javascript">

function ajaxFunction()
{
var xmlHttp;

try
    {
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
catch (e)
    {

  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
    }
}
</script>
2如何使用XMLHttpRequest 对象与服务器通信。XMLHTTPRequest对象的三个重要属性。

xmlHttp.onreadystatechange=function()
  {
  if(xmlHttp.readyState==4)
    {
    // 从服务器的response获得数据

document.myForm.time.value=xmlHttp.responseText;

    }
  }
 3向服务器请求数据

 

要想把请求发送到服务器,我们就需要使用 open() 方法和 send() 方法。

open() 方法需要三个参数。第一个参数定义发送请求所使用的方法(GET 还是 POST)。第二个参数规定服务器端脚本的 URL。第三个参数规定应当对请求进行异步地处理。

 

 

send() 方法可将请求送往服务器。如果我们假设 HTML 文件和 ASP 文件位于相同的目录,那么代码是这样的:

xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);

 参考资料:http://www.w3school.com.cn/ajax/ajax_server.asp

http://www.w3school.com.cn/aspnet/index.asp

http://blog.csdn.net/richcem/archive/2010/05/05/5558369.aspx

分类:

技术点:

相关文章: