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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题