通過XMLHttpRequest和jQuery實現(xiàn)ajax的幾種方式
示例一:利用Ajax來動態(tài)獲取時間的例子。
HTML代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript" src="Scripts/jwy.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" name="textbox" id="text1" />
<input type="button" name="button" id="Button1" value="顯示時間" onclick="btnclick()" />
</div>
</form>
</body>
</html>
創(chuàng)建一個“一般處理程序”來處理前臺請求,返回系統(tǒng)當前時間:
Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System.Text;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(ShowTime());
}
public bool IsReusable {
get {
return false;
}
}
public static string ShowTime()
{
return DateTime.Now.ToString();
}
}
js代碼:
function btnclick() {
var httprequest = null;
// 初始化XMLHttpRequest對象
if (window.XMLHttpRequest) {
// Firefox等現(xiàn)代瀏覽器中的XMLHttpRequest對象創(chuàng)建
httprequest = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
// IE中的XMLHttpRequest對象創(chuàng)建
httprequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if (!httprequest) {
alert("創(chuàng)建httprequest對象出現(xiàn)異常!");
}
httprequest.open("POST", "Handler.ashx", true);
//httprequest向handler發(fā)送post請求,最后參數(shù)是設定是否為異步請求,true為異步,false為同步
httprequest.onreadystatechange = function () {
//指定onreadystatechange事件句柄對應的函數(shù)
if (httprequest.readyState == 4) {
//4代表服務器返回完成
if (httprequest.status == 200) {
//200代表成功了
document.getElementById("text1").value = httprequest.responseText;
//responsetext表示服務器返回的文本,還有一種方式是responseXML是為了獲取服務器返回的xml
}
else {
alert("AJAX服務器返回錯誤!");
}
}
}
httprequest.send();
//在這里才真正的向服務器端發(fā)送請求
}
我們用jquery來前臺js代碼會變得十分簡潔:
基于jquery編寫的js代碼:
注意:HTML代碼要把button的onclick事件去掉,因為我們直接在js用了事件綁定。
$(document).ready(function () {
//button1綁定事件
$("#Button1").bind("click", function () {
$.ajax({
url: "Handler.ashx",
type: "POST",
success: function (data) {
//$("#text1").val(data);
document.getElementById("text1").value = data;
}
});
});
});
不得不說jquery“簡約而不簡單”……
jquery中的$.ajax集合了get、post方法,默認的是get。
如果直接用POST的話,代碼更簡單
$(document).ready(function () {
//button1綁定事件
$("#Button1").bind("click", function () {
$.post("Handler.ashx", function (data) {
document.getElementById("text1").value = data;
});
});
});
示例二:
一、XMLHttpRequest實現(xiàn)獲取數(shù)據(jù)
不使用jQuery實現(xiàn)頁面不刷新獲取內(nèi)容的方式,我們這里采用XMLHttpRequest原生代碼實現(xiàn);
js代碼如下:
//1.獲取a節(jié)點,并為其添加Oncilck響應函數(shù)
document.getElementsByTagName("a")[0].onclick = function(){
//3、創(chuàng)建一個XMLHttpRequest();
var request = new XMLHttpRequest();
//4、準備發(fā)送請求的數(shù)據(jù)url
var url = this.href;
var method = "GET";
//5、調(diào)用XMLHttpRequest對象的open方法
request.open(method,url);
//6、調(diào)用XMLHttpRequest對象的send方法
request.send(null);
//7、為XMLHttpRequest對象添加onreadystatechange 響應函數(shù)
request.onreadystatechange = function(){
//8、判斷響應是否完成:XMLHttpRequest 對象的readystate的屬性值為4的時候
if(request.readyState == 4){
//9、在判斷響應是否可用:XMLHttpRequest 對象status 屬性值為200
if(request.status == 200){
//10、響應結果
alert(request.responseText);
}
}
}
//2、取消a節(jié)點的額默認行為
return false;
}
插入HTML代碼:
<a href = "hello.txt">點擊獲取文本內(nèi)容</a>
二、jQuery實現(xiàn)ajax獲取信息
這個例子是動態(tài)的從后臺獲取數(shù)據(jù)來改變下拉按鈕的內(nèi)容;
js代碼如下:
function bindCarteam0(){
//通過URL請求數(shù)據(jù)
var URL = <select:link page="/xiaoshouwl.do?method=getCarteamList"/>;
$.ajax({
url:URL,
type:'GET',
dataType: "json",
success:function(html){
var str="<option value='-1'>全部</option>";
for(var i=0;i<html.length;i++){
str+="<option value='"+html[i].id+"'>"+html[i].name+"</option>";
}
$("#carteam_code").empty().html(str);
}
});
}
HTML代碼如下:
<select:select property="carteam_code" styleId="carteam_code" style="width:150px">
<select:option value="-1">全部</select:option>
</select:select>
其中type類型有get和post兩種;
post 可以傳輸?shù)臄?shù)據(jù)量比較大,get有字節(jié)限制;
- JQuery的ajax獲取數(shù)據(jù)后的處理總結(html,xml,json)
- AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)
- 用Ajax讀取xml文件的簡單例子
- Jquery Ajax學習實例 向頁面發(fā)出請求,返回XML格式數(shù)據(jù)
- AJAX中同時發(fā)送多個請求XMLHttpRequest對象處理方法
- JS通過ajax動態(tài)讀取xml文件內(nèi)容的方法
- AJAX 常用函數(shù)創(chuàng)建XMLHTTP對象,區(qū)別IE,Mozilla瀏覽器
- javascript解析ajax返回的xml和json格式數(shù)據(jù)實例詳解
- firefox下jquery ajax返回object XMLDocument處理方法
- 用Ajax讀取XML格式的數(shù)據(jù)
- AJAX+JSP實現(xiàn)讀取XML內(nèi)容并按排列顯示輸出的方法示例
相關文章
jQuery插件HighCharts繪制2D金字塔圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制2D金字塔圖效果,結合實例形式分析了jQuery使用HighCharts插件繪制金字塔圖效果的操作步驟與相關實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
在jquery中綁定事件我們可以使用click、change、mouseout、.bind(),live等待事件來操作,下面我來給大家介紹jQuery使用向DOM元素綁定事件實現(xiàn)程序相關實例,有需要了解的同不可進入?yún)⒖肌?/div> 2015-04-04
jQuery刪除節(jié)點的三個方法即remove()detach()和empty()
jQuery提供了三種刪除節(jié)點的方法,即remove(),detach()和empty(),下面為大家詳細介紹下jQuery刪除節(jié)點三個方法的具體使用2013-12-12
使用微信內(nèi)置瀏覽器點擊下拉框出現(xiàn)頁面亂跳轉現(xiàn)象(iphone),該怎么辦
這篇文章主要介紹了使用微信內(nèi)置瀏覽器點擊下拉框出現(xiàn)頁面亂跳轉現(xiàn)象(iphone),該怎么辦的相關資料,需要的朋友可以參考下2016-01-01
jquery UI Datepicker時間控件的使用方法(基礎版)
這篇文章主要介紹了jquery ui datepicker時間控件的使用方法,需要的朋友可以參考下2015-11-11最新評論

