• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    Ajax 核心框架函數及例子

    來源:懂視網 責編:小采 時間:2020-11-27 22:52:30
    文檔

    Ajax 核心框架函數及例子

    Ajax 核心框架函數及例子:核心ajax(options)函數中,包含了建立xmlhttprequest,提取數據,判斷是否回復成功等,基本滿足了日常需求。 代碼如下:// A generic function for performming AJAX requests // It takes one argument, which is a
    推薦度:
    導讀Ajax 核心框架函數及例子:核心ajax(options)函數中,包含了建立xmlhttprequest,提取數據,判斷是否回復成功等,基本滿足了日常需求。 代碼如下:// A generic function for performming AJAX requests // It takes one argument, which is a

    核心ajax(options)函數中,包含了建立xmlhttprequest,提取數據,判斷是否回復成功等,基本滿足了日常需求。
    代碼如下:

    // A generic function for performming AJAX requests
    // It takes one argument, which is an object that contains a set of options
    // All of which are outline in the comments, below
    function ajax( options ) {
    // Load the options object with defaults, if no
    // values were provided by the user
    options = {
    // The type of HTTP Request
    type: options.type || "POST",
    // The URL the request will be made to
    url: options.url || "",
    // How long to wait before considering the request to be a timeout
    timeout: options.timeout || 5000,
    // Functions to call when the request fails, succeeds,
    // or completes (either fail or succeed)
    onComplete: options.onComplete || function(){},
    onError: options.onError || function(){},
    onSuccess: options.onSuccess || function(){},
    // The data type that'll be returned from the server
    // the default is simply to determine what data was returned from the
    // and act accordingly.
    data: options.data || ""
    };
    // Create the request object
    var xml = new XMLHttpRequest();
    // Open the asynchronous POST request
    //xml.open("GET", "/some/url.cgi", true);
    xml.open("GET",options.url, true);
    // We're going to wait for a request for 5 seconds, before giving up
    var timeoutLength = 5000;
    // Keep track of when the request has been succesfully completed
    var requestDone = false;
    // Initalize a callback which will fire 5 seconds from now, cancelling
    // the request (if it has not already occurred).
    setTimeout(function(){
    requestDone = true;
    }, timeoutLength);
    // Watch for when the state of the document gets updated
    xml.onreadystatechange = function(){
    // Wait until the data is fully loaded,
    // and make sure that the request hasn't already timed out
    if ( xml.readyState == 4 && !requestDone ) {
    // Check to see if the request was successful
    if ( httpSuccess( xml ) ) {
    // Execute the success callback with the data returned from the server
    options.onSuccess( httpData( xml, options.type ) );
    // Otherwise, an error occurred, so execute the error callback
    } else {
    options.onError();
    }
    // Call the completion callback
    options.onComplete();
    // Clean up after ourselves, to avoid memory leaks
    xml = null;
    }
    };
    // Establish the connection to the server
    xml.send();
    // Determine the success of the HTTP response
    function httpSuccess(r) {
    try {
    // If no server status is provided, and we're actually
    // requesting a local file, then it was successful
    return !r.status && location.protocol == "file:" ||
    // Any status in the 200 range is good
    ( r.status >= 200 && r.status < 300 ) ||
    // Successful if the document has not been modified
    r.status == 304 ||
    // Safari returns an empty status if the file has not been modified
    navigator.userAgent.indexOf("Safari") >= 0 && typeof r.status == "undefined";
    } catch(e){}
    // If checking the status failed, then assume that the request failed too
    return false;
    }
    // Extract the correct data from the HTTP response
    function httpData(r,type) {
    // Get the content-type header
    var ct = r.getResponseHeader("content-type");
    // If no default type was provided, determine if some
    // form of XML was returned from the server
    var data = !type && ct && ct.indexOf("xml") >= 0;
    // Get the XML Document object if XML was returned from
    // the server, otherwise return the text contents returned by the server
    data = type == "xml" || data ? r.responseXML : r.responseText;
    // If the specified type is "script", execute the returned text
    // response as if it was JavaScript
    if ( type == "script" )
    eval.call( window, data );
    // Return the response data (either an XML Document or a text string)
    return data;
    }
    }

    在同等目錄中,我們可以建立一個rss.xml文件,用這個函數來訪問。
    rss.xml如下:
    代碼如下:

    <titles>
    <title>
    緣份
    </title>
    <title>
    月亮
    </title>
    <title>
    緣份月亮
    </title>
    </titles>

    再建立一個html文檔,調用它,就能看到rss.xml中的內容就能被訪問到。
    整體看看,其實真的比較簡潔和簡單。不僅是能訪問xml格式文件,html,.js格式的文件都可以調用的;
    這些都可以在本地建立對應的文件,進行調用,都可以實現。

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    Ajax 核心框架函數及例子

    Ajax 核心框架函數及例子:核心ajax(options)函數中,包含了建立xmlhttprequest,提取數據,判斷是否回復成功等,基本滿足了日常需求。 代碼如下:// A generic function for performming AJAX requests // It takes one argument, which is a
    推薦度:
    標簽: aj 實例 函數
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 无码国产亚洲日韩国精品视频一区二区三区| 国产精品午夜无码AV天美传媒| 国产精品成人99久久久久| 亚洲Av无码精品色午夜| 国产亚洲精品无码专区| 久久99国产精品久久99果冻传媒| 亚洲欧洲自拍拍偷精品 美利坚| 国产精品99久久久久久猫咪 | 久久精品aⅴ无码中文字字幕重口| 国产精品无码素人福利| www.亚洲精品| 国产精品免费观看调教网| 一本一道精品欧美中文字幕 | 黑巨人与欧美精品一区| 青青草国产精品久久| 国产成人精品999在线观看| 无码精品人妻一区二区三区中| 久久996热精品xxxx| 国产成人精品无人区一区| 国产精品亚洲精品观看不卡| 精品久久久久香蕉网| 亚洲国产精品无码久久久不卡| 麻豆国产高清精品国在线| 国产精品综合久成人| 成人国产精品动漫欧美一区| 欧美一卡2卡3卡四卡海外精品| 精品免费视在线观看| 久久亚洲精品视频| 久久夜色精品国产亚洲| 久久精品国产亚洲欧美| 精品亚洲综合在线第一区| 国内精品久久久久久野外| 国产精品黄网站| 精品一区二区三区免费| 性色精品视频网站在线观看 | 国产三级精品三级| 国产精品狼人久久久久影院| 国产精品女同一区二区久久| 国产三级精品三级| 麻豆国内精品欧美在线| 亚洲AV蜜桃永久无码精品|