• 在AJAX中使用POST方法(本文纯属转载)


    Using POST method in XMLHTTPRequest(Ajax)

    Usually only the GET method is used while creating Ajax apps. But there are several occasions when POST is necessary when creating a ajax request. This could be for several reasons. For example, POST request are considered more secure than GET request as creating a POST request is relatively harder than creating a GET request.

    Requirements

    • Create a XMLHTTPRequest Object that uses the POST method.
    • See if the arguments passed to it appear in the '$_POST' array in PHP.

    Code

    XMLHTTPRequest Object

    For the sake of simplicity, we are going to create the XMLHTTPRequest object using the Firefox supported ' XMLHttpRequest()' function. I believe that you know the proper way of creating a cross-browser XMLHttpRequest object. If not, learn that first.

    var http = new XMLHttpRequest();

    Using GET method

    Now we open a connection using the GET method.

    
    var url = "get_data.php";
    var params = "lorem=ipsum&name=binny";
    http.open("GET", url+"?"+params, true);
    http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
    }
    }
    http.send(null);
    

    I really hope that this much is clear for you - I am assuming that you know a bit of Ajax coding. If you don't, please read a ajax tutorial that explains these parts before continuing.

    POST method

    We are going to make some modifications so POST method will be used when sending the request...

    
    var url = "get_data.php";
    var params = "lorem=ipsum&name=binny";
    http.open("POST", url, true);
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
    }
    }
    http.send(params);
    

    The first change(and the most obvious one) is that I changed the first argument of the open function from GET to POST. Also notice the difference in the second argument - in the GET method, we send the parameters along with the url separated by a '?' character...

    http.open("GET",url+"?"+params, true);

    But in the POST method we will use just the url as the second argument. We will send the parameters later.

    http.open("POST", url, true);

    Some http headers must be set along with any POST request. So we set them in these lines...

    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    With the above lines we are basically saying that the data send is in the format of a form submission. We also give the length of the parameters we are sending.

    http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
    }
    }

    We set a handler for the 'ready state' change event. This is the same handler we used for the GET method. You can use the http.responseText here - insert into a div using innerHTML(AHAH), eval it(JSON) or anything else.

    http.send(params);

    Finally, we send the parameters with the request. The given url is loaded only after this line is called. In the GET method, the parameter will be a null value. But in the POST method, the data to be send will be send as the argument of the send function. The params variable was declared in the second line as "lorem=ipsum&name=binny" - so we send two parameters - 'lorem' and 'name' with the values 'ipsum' and 'binny' respectively.

    That's it. If you wish to see the working of this script, I have set up a demo page for Ajax using post.

  • 相关阅读:
    luogu P1162 填涂颜色 x
    【説明する】素数
    codevs 4064 组合 x
    codevs 2039 骑马修栅栏 USACO x
    codevs 2038 香甜的黄油x+luogu P1828 x
    [HIHO1079]离散化(线段树、染色)
    [HIHO1196]高斯消元·二(高斯消元、枚举自由变元)
    hihoCoder太阁最新面经算法竞赛17
    [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
    [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)
  • 原文地址:https://www.cnblogs.com/kingclever/p/1033187.html
Copyright © 2020-2023  润新知