close
首先 先將這 function 加到 code 裡
 
******************************************************************
function getXMLHttpRequest(isAsync, url, method, params)
{
    var objXMLHttp = null;
    if (window.XMLHttpRequest)
    {
        // Firefox, Opera 8.0+, Safari
        objXMLHttp = new XMLHttpRequest();
        if (objXMLHttp.overrideMimeType)
        {
            objXMLHttp.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject)
    {
        // Internet Explorer
        try
        {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e)
        {
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!objXMLHttp)
    {
        return null;
    }
   
 objXMLHttp.open(method, url, isAsync);
    if (method == "POST")
    {
        objXMLHttp.setRequestHeader("Content-type",
                                    "application/x-www-form-urlencoded");
        objXMLHttp.setRequestHeader("Content-length", params.length);
        objXMLHttp.setRequestHeader("Connection", "close");
    }
   
    this.obj = objXMLHttp;
    this.method = method;
    this.params = params;
    this.onreadystatechange = null;
    this.send = function()
    {
        this.obj.onreadystatechange = this.onreadystatechange;
        try
        {
            if (this.method == "POST")
            {
                this.obj.send(this.params);
            } else if (this.method == "GET")
            {
                this.obj.send(null);
            }
        } catch (e)
        {
         return e.message;
        }
    }
}
 
******************************************************************
使用方法如下:
 
function reqUserProfile (vid, gid, lang)
{
    var isAsync = false; // 設定非同步. ture or false
    var url = G_ROUTE + "/profileservice.htm"; // 目標的 URL
    var params = "perform=getprofile&vid=" + vid; // post 要帶的 params
    var method = "POST"; // 設定 method. POST or GET , 使用 GET 時不需要帶 params
    var request = new getXMLHttpRequest(isAsync, url, method, params);
    request.onreadystatechange = function ()
    {
        if (request.obj.readyState == 4)
        {
   if (request.obj.status == 200)
            {
                // 200 OK , 收到 response
                var Xml = request.obj.responseXML;
               
            } else
            {
                // 200 以外的情況 , 都在這裡處理
            }
        }
    }
    request.send();
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 雪 薄草 的頭像
    雪 薄草

    雪 薄草的部落格

    雪 薄草 發表在 痞客邦 留言(0) 人氣()