jquery ajax 應用返回類型是html json


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM