weixin_33743661 2016-04-26 06:42 采纳率: 0%
浏览 44

关于ajax的困惑

There is a way to send or get request using ajax which allows to send data without page reload like everything happens behind the scene i found a script for doing that but some of the functions within aer confusing can anyone explain me what are they and why we are using those

data.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
data.onreadystatechange = function () {
    if(data.readystate == 4 && data.status == 200) {
        data.return_data = data.responseText;
        document.getElementById("response").innerHTML = return_data;
    }
}

data.setRequestHeader What is the use of this function and why we are using this onreadystatechange same for this and not understanding why if condition is used

Help would be highly appreciated

  • 写回答

1条回答 默认 最新

  • weixin_33716941 2016-04-26 07:20
    关注

    You are using XMLHttpRequest to send/get the data.

    The function setRequestHeader is used to set value of header params before sending request to server.

    The condition if(data.readystate == 4 && data.status == 200) is used to check what is the status and state of request. By using if condition you can verify the success of your request and take needful action after success. Below is the list of possible status and state.

    Possible states with description

    • 0 The request is not initialized
    • 1 The request has been set up
    • 2 The request has been sent
    • 3 The request is in process
    • 4 The request is complete

    to see list of all possible status codes please refer to developer.mozilla.org

    评论

报告相同问题?