Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
JAX 是與服務(wù)器交換數(shù)據(jù)的技術(shù),它在不重載全部頁面的情況下,實現(xiàn)了對部分網(wǎng)頁的更新。
jQuery AJAX 實例
使用 jQuery AJAX 修改文本內(nèi)容
獲取外部內(nèi)容
什么是 AJAX?
AJAX = 異步 JavaScript 和 XML(Asynchronous JavaScript and XML)。
簡短地說,在不重載整個網(wǎng)頁的情況下,AJAX 通過后臺加載數(shù)據(jù),并在網(wǎng)頁上進行顯示。
使用 AJAX 的應(yīng)用程序案例:谷歌地圖、騰訊微博、優(yōu)酷視頻、人人網(wǎng)等等。
您可以在我們的 jQuery Ajax 參考手冊學會 jQuery Ajax 的具體應(yīng)用。
您可以在我們的 AJAX 教程中學到更多有關(guān) AJAX 的知識。
關(guān)于 jQuery 與 AJAX
jQuery 提供多個與 AJAX 有關(guān)的方法。
通過 jQuery AJAX 方法,您能夠使用 HTTP Get 和 HTTP Post 從遠程服務(wù)器上請求文本、HTML、XML 或 JSON - 同時您能夠把這些外部數(shù)據(jù)直接載入網(wǎng)頁的被選元素中。
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
如果沒有 jQuery,AJAX 編程還是有些難度的。
編寫常規(guī)的 AJAX 代碼并不容易,因為不同的瀏覽器對 AJAX 的實現(xiàn)并不相同。這意味著您必須編寫額外的代碼對瀏覽器進行測試。不過,jQuery 團隊為我們解決了這個難題,我們只需要一行簡單的代碼,就可以實現(xiàn) AJAX 功能。
日常工作中(主要使用ASP.net),我需要編寫很多JavaScript代碼。我做的最重復的任務(wù)之一是jQuery Ajax調(diào)用。你看:
$.ajax({
type: "POST",
url: "MyPage.aspx/MyWebMethod",
data: "{parameter:value,parameter:value}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
//function called successfull
},
error: function(msg) {
//some error happened
}
});
我不知道對您來說是否相同,但是對我來說用這種語法編寫調(diào)用太麻煩了。而且,以前必須按照已知規(guī)則使用參數(shù)構(gòu)建字符串以傳遞給WebMethod:字符串必須用引號傳遞,數(shù)字不能傳遞,等等。
因此,我決定創(chuàng)建一個小而有用的JavaScript類來幫助我有了這個特定的問題,現(xiàn)在我已經(jīng)完成了jQuery Ajax調(diào)用,這對我來說非常友好。
在類構(gòu)造函數(shù)中,我傳遞頁面名稱,方法名稱以及成功和錯誤函數(shù)。以后,我會根據(jù)需要對addParam方法進行盡可能多的調(diào)用。最后,我調(diào)用run方法進行Ajax調(diào)用。成功函數(shù)和錯誤函數(shù)必須分別編寫。
參數(shù)根據(jù)其類型進行處理。如果參數(shù)是字符串,則使用引號。如果是數(shù)字,我不會。日期參數(shù)是一種特殊情況。在這種情況下,我使用JavaScript日期對象的getTime()函數(shù),該函數(shù)給了我自1970年1月1日以來的日期的毫秒數(shù)。后來,我將該值轉(zhuǎn)換為UTC時間,這樣我得到了一個最終的毫秒數(shù),即我可以將Int64值傳遞給我的VB.net(或C#)代碼,用它來重建.Net中的日期值。
這是我的jAjax類的完整列表(末尾帶有日期幫助器功能):
function jAjax(pageName, methodName, successFunc, errorFunc) {
//stores the page name
this.pageName = pageName;
//stores the method name
this.methodName = methodName;
//stores the success function
this.successFunc = successFunc;
//stores the error function
this.errorFunc = errorFunc;
//initializes the parameter names array
this.paramNames = new Array();
//initializes the parameter values array
this.paramValues = new Array();
//method for add a new parameter (simply pushes to the names and values arrays)
this.addParam = function(name, value) {
this.paramNames.push(name);
this.paramValues.push(value);
}
//method to run the jQuery ajax request
this.run = function() {
//initializes the parameter data string
var dataStr = '{';
//iterate thru the parameters arrays to compose the parameter data string
for (var k = 0; k < this.paramNames.length; k++) {
//append the parameter name
dataStr += this.paramNames[k] + ':';
if (typeof this.paramValues[k] == 'string') {
//string parameter, append between quotes
dataStr += '"' + this.paramValues[k] + '",';
} else if (typeof this.paramValues[k] == 'number') {
//number parameter, append "as-is" calling toString()
dataStr += this.paramValues[k].toString() + ',';
} else if (typeof this.paramValues[k] == 'object') {
if (this.paramValues[k].getTime != undefined) {
//date value
//call to my getUtcTime function to get the number of
//milliseconds (since january 1, 1970) in UTC format
//and append as a number
dataStr += getUtcTime(this.paramValues[k]).toString() + ',';
} else {
//object value
//because I don't know what's this, append the toString()
//output
dataStr += '"' + this.paramValues[k].toString() + '",';
}
}
}
//if some parameter added, remove the trailing ","
if (dataStr.length > 1) dataStr = dataStr.substr(0, dataStr.length - 1);
//close the parameter data string
dataStr += '}';
//do the jQuery ajax call, using the parameter data string
//and the success and error functions
$.ajax({
type: "POST",
url: this.pageName + "/" + this.methodName,
data: dataStr,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
successFunc(msg);
},
error: function(msg) {
errorFunc(msg);
}
});
}
}
function getUtcTime(dateValue) {
//get the number of milliseconds since january 1, 1970
var time = dateValue.getTime();
//get the UTC time offset in minutes. if you created your date with
//new Date(), maybe this contains a value. but if you compose the date
//by yourself (i.e. var myDate = new Date(1984,5,21,12,53,11)) this will
//be zero
var minutes = dateValue.getTimezoneOffset() * -1;
//get the milliseconds value
var ms = minutes * 60000;
//add to the original milliseconds value so we get the GMT exact value
return time + ms;
}
這是使用的語法:
var ajaxCall = new jAjax('MyPage.aspx','MyWebMethod',successFunc,errorFunc);
ajaxCall.addParam('s','this is a string');
ajaxCall.addParam('n',34);
ajaxCall.addParam('d',new Date());
ajaxCall.run();
function successFunc(msg) {
...
}
function errorFunc(msg) {
}
另外一個好處是,您可以將成功和錯誤功能重新用于幾個ajax調(diào)用。
希望對您有幫助!!隨時在您的應(yīng)用程序中使用jAjax類。
JAX 是一種與服務(wù)器交換數(shù)據(jù)的技術(shù),可以在補充在整個頁面的情況下更新網(wǎng)頁的一部分。接下來通過本文給大家介紹ajax一些常用方法,大家有需要可以一起學習。
1.url:
要求為String類型的參數(shù),(默認為當前頁地址)發(fā)送請求的地址。
2.type:
要求為String類型的參數(shù),請求方式(post或get)默認為get。注意其他http請求方法,例如put和delete也可以使用,但僅部分瀏覽器支持。
3.timeout:
要求為Number類型的參數(shù),設(shè)置請求超時時間(毫秒)。此設(shè)置將覆蓋$.ajaxSetup()方法的全局設(shè)置。
4.async:
要求為Boolean類型的參數(shù),默認設(shè)置為true,所有請求均為異步請求。如果需要發(fā)送同步請求,請將此選項設(shè)置為false。注意,同步請求將鎖住瀏覽器,用戶其他操作必須等待請求完成才可以執(zhí)行。
5.cache:
要求為Boolean類型的參數(shù),默認為true(當dataType為script時,默認為false),設(shè)置為false將不會從瀏覽器緩存中加載請求信息。
6.data:
要求為Object或String類型的參數(shù),發(fā)送到服務(wù)器的數(shù)據(jù)。如果已經(jīng)不是字符串,將自動轉(zhuǎn)換為字符串格式。get請求中將附加在url后。防止這種自動轉(zhuǎn)換,可以查看 processData選項。對象必須為key/value格式,例如{foo1:"bar1",foo2:"bar2"}轉(zhuǎn)換為&foo1=bar1&foo2=bar2。如果是數(shù)組,JQuery將自動為不同值對應(yīng)同一個名稱。例如{foo:["bar1","bar2"]}轉(zhuǎn)換為&foo=bar1&foo=bar2。
7.dataType:
要求為String類型的參數(shù),預期服務(wù)器返回的數(shù)據(jù)類型。如果不指定,JQuery將自動根據(jù)http包mime信息返回responseXML或responseText,并作為回調(diào)函數(shù)參數(shù)傳遞。可用的類型如下:
xml:返回XML文檔,可用JQuery處理。
html:返回純文本HTML信息;包含的script標簽會在插入DOM時執(zhí)行。
script:返回純文本JavaScript代碼。不會自動緩存結(jié)果。除非設(shè)置了cache參數(shù)。注意在遠程請求時(不在同一個域下),所有post請求都將轉(zhuǎn)為get請求。
json:返回JSON數(shù)據(jù)。
jsonp:JSONP格式。使用SONP形式調(diào)用函數(shù)時,例如myurl?callback=?,JQuery將自動替換后一個“?”為正確的函數(shù)名,以執(zhí)行回調(diào)函數(shù)。
text:返回純文本字符串。
8.beforeSend:
要求為Function類型的參數(shù),發(fā)送請求前可以修改XMLHttpRequest對象的函數(shù),例如添加自定義HTTP頭。在beforeSend中如果返回false可以取消本次ajax請求。XMLHttpRequest對象是惟一的參數(shù)。
function(XMLHttpRequest){
this; //調(diào)用本次ajax請求時傳遞的options參數(shù)
}
9.complete:
要求為Function類型的參數(shù),請求完成后調(diào)用的回調(diào)函數(shù)(請求成功或失敗時均調(diào)用)。參數(shù):XMLHttpRequest對象和一個描述成功請求類型的字符串。
function(XMLHttpRequest, textStatus){
this; //調(diào)用本次ajax請求時傳遞的options參數(shù)
}
10.success:
要求為Function類型的參數(shù),請求成功后調(diào)用的回調(diào)函數(shù),有兩個參數(shù)。
(1)由服務(wù)器返回,并根據(jù)dataType參數(shù)進行處理后的數(shù)據(jù)。
(2)描述狀態(tài)的字符串。
function(data, textStatus){
//data可能是xmlDoc、jsonObj、html、text等等
this; //調(diào)用本次ajax請求時傳遞的options參數(shù)
}
11.error:
要求為Function類型的參數(shù),請求失敗時被調(diào)用的函數(shù)。該函數(shù)有3個參數(shù),即XMLHttpRequest對象、錯誤信息、捕獲的錯誤對象(可選)。ajax事件函數(shù)如下:
function(XMLHttpRequest, textStatus, errorThrown){
//通常情況下textStatus和errorThrown只有其中一個包含信息
this; //調(diào)用本次ajax請求時傳遞的options參數(shù)
}
12.contentType:
要求為String類型的參數(shù),當發(fā)送信息至服務(wù)器時,內(nèi)容編碼類型默認為"application/x-www-form-urlencoded"。該默認值適合大多數(shù)應(yīng)用場合。
13.dataFilter:
要求為Function類型的參數(shù),給Ajax返回的原始數(shù)據(jù)進行預處理的函數(shù)。提供data和type兩個參數(shù)。data是Ajax返回的原始數(shù)據(jù),type是調(diào)用jQuery.ajax時提供的dataType參數(shù)。函數(shù)返回的值將由jQuery進一步處理。
function(data, type){
//返回處理后的數(shù)據(jù)
return data;
}
14.dataFilter:
要求為Function類型的參數(shù),給Ajax返回的原始數(shù)據(jù)進行預處理的函數(shù)。提供data和type兩個參數(shù)。data是Ajax返回的原始數(shù)據(jù),type是調(diào)用jQuery.ajax時提供的dataType參數(shù)。函數(shù)返回的值將由jQuery進一步處理。
function(data, type){
//返回處理后的數(shù)據(jù)
return data;
}
15.global:
要求為Boolean類型的參數(shù),默認為true。表示是否觸發(fā)全局ajax事件。設(shè)置為false將不會觸發(fā)全局ajax事件,ajaxStart或ajaxStop可用于控制各種ajax事件。
16.ifModified:
要求為Boolean類型的參數(shù),默認為false。僅在服務(wù)器數(shù)據(jù)改變時獲取新數(shù)據(jù)。服務(wù)器數(shù)據(jù)改變判斷的依據(jù)是Last-Modified頭信息。默認值是false,即忽略頭信息。
17.jsonp:
要求為String類型的參數(shù),在一個jsonp請求中重寫回調(diào)函數(shù)的名字。該值用來替代在"callback=?"這種GET或POST請求中URL參數(shù)里的"callback"部分,例如{jsonp:'onJsonPLoad'}會導致將"onJsonPLoad=?"傳給服務(wù)器。
18.username:
要求為String類型的參數(shù),用于響應(yīng)HTTP訪問認證請求的用戶名。
19.password:
要求為String類型的參數(shù),用于響應(yīng)HTTP訪問認證請求的密碼。
20.processData:
要求為Boolean類型的參數(shù),默認為true。默認情況下,發(fā)送的數(shù)據(jù)將被轉(zhuǎn)換為對象(從技術(shù)角度來講并非字符串)以配合默認內(nèi)容類型"application/x-www-form-urlencoded"。如果要發(fā)送DOM樹信息或者其他不希望轉(zhuǎn)換的信息,請設(shè)置為false。
21.scriptCharset:
要求為String類型的參數(shù),只有當請求時dataType為"jsonp"或者"script",并且type是GET時才會用于強制修改字符集(charset)。通常在本地和遠程的內(nèi)容編碼不同時使用。
案例代碼:
$(function(){
$('#send').click(function(){
$.ajax({
type: "GET",
url: "test.json",
data: {username:$("#username").val(), content:$("#content").val()},
dataType: "json",
success: function(data){
$('#resText').empty(); //清空resText里面的所有內(nèi)容
var html = '';
$.each(data, function(commentIndex, comment){
html += '<div class="comment"><h6>' + comment['username']
+ ':</h6><p class="para"' + comment['content']
+ '</p></div>';
});
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。