zoukankan      html  css  js  c++  java
  • 同步异步请求区别

     
     
    请求方式,分为GET与POST:
     
    GET
     
    最为常见的HTTP请求,普通上网浏览页面就是GET。GET方式的参数请求直接跟在URL后,以问号开始。(JS中用window.location.search获得)。参数可以用encodeURIComponent进行编码,使用方式:
     

    var EnParam = encodeURIComponent(param);

    URL只支持大约2K的长度,即2048字符数;使用GET进行AJAX请求时候会缓存导致出现的页面不是正确的,一般方法加random参数值;ajax.send(null)。

    POST

    向服务器提交数据用到。

    需要将form表单中的值先取出转换成字符串,用&符号连接,(同GET传参数一样);提交数据量2GB ;使用ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'),处理提交的字符串;ajax.send(strings),这个strings表示form中需要提交的内容,例如a=1&b=2类似这样的字符串。

    同步与异步:
    举个例子:普通B/S模式(同步)AJAX技术(异步) 同步:提交请求->等待服务器处理->处理完毕返回 这个期间客户端浏览器不能干任何事 异步: 请求通过事件触发->服务器处理(这是浏览器仍然可以作其他事情)->处理完毕

    ajax.open方法中,第3个参数是设同步或者异步。prototype等js类库一般都默认为异步,即设为true。先说下同步的情况下,js会等待请求返回,获取status。不需要onreadystatechange事件处理函数。而异步则需要onreadystatechange事件处理,且值为4再正确处理下面的内容。

    //同步传输模式
     
    function RequestByGet(nProducttemp,nCountrytemp)
    {
        var xmlhttp
     
        if (window.XMLHttpRequest)  
        {  
             //isIE   =   false;  
             xmlhttp   =   new   XMLHttpRequest();  
        }  
        else if (window.ActiveXObject)
        {  
             //isIE   =   true;  
             xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");  
        }
                     
        //Web page location.
        var URL="http://www.baidu.com/;
        xmlhttp.open("GET",URL, false);
        //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=Shift_JIS")
        xmlhttp.send(null);
        var result = xmlhttp.status;
       
        //OK
        if(result==200)
        {
            document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;
        }
        xmlhttp = null;
    }
     
    
    //异步传输模式
    var xmlhttp
     
    function RequestByGet(nProducttemp,nCountrytemp)
    {
        if (window.XMLHttpRequest)  
        {  
             //isIE   =   false;  
             xmlhttp   =   new   XMLHttpRequest();  
        }  
        else if (window.ActiveXObject)
        {  
             //isIE   =   true;  
             xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");  
        }
                     
        //Web page location.
        var URL="http://www.baidu.com/";
        xmlhttp.open("GET",URL, true);
        xmlhttp.onreadystatechange = handleResponse;
        //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=UTF-8")
        xmlhttp.send(null);  
    }
     
    function handleResponse()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status==200)
        {
            document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;
            xmlhttp = null;
        }
    }
  • 相关阅读:
    ZOJ 2158 Truck History
    Knight Moves (zoj 1091 poj2243)BFS
    poj 1270 Following Orders
    poj 2935 Basic Wall Maze (BFS)
    Holedox Moving (zoj 1361 poj 1324)bfs
    ZOJ 1083 Frame Stacking
    zoj 2193 Window Pains
    hdu1412{A} + {B}
    hdu2031进制转换
    openjudge最长单词
  • 原文地址:https://www.cnblogs.com/nidakun/p/2944744.html
Copyright © 2011-2022 走看看