zoukankan      html  css  js  c++  java
  • Ajax学习笔记——XMLHttpRequest对象发送request

     

    Ajax学习笔记——XMLHttpRequest对象发送request

     

    open 函数

     

    假设已经创建了一个XMLHttpRequest对象——request;
    如果浏览器向服务器请求一个页面则使用open()函数
    request.open("GET","file")
    request.open("GET","search.php?query=Jonh")

    void open(string method, string url, boolean Asynchronous string username, string password)


    第一个参数表明提交方法:有GET和POST
    第二个参数是页面地址:可以用相对地址如 “../myfile.html”或者绝对地址“/files/myfile.html”
    第三个参数表明是否使用异步,默认是true。如果是异步,脚本会继续执行下去而不等待服务器响应;否则它会停在那里,等响应。
    第四个第五个参数是用户名和密码。一般从表单中传递过来。

     

    send 函数

    open只是函数确定了Ajax request的细节,所以还要用send函数发送request
    由于GET函数没有发送数据,所以参数为null
    request.send(null);

     

    若用POST方法做查询向服务器提交
    request.send("name=Jeremy+Keith&message=Hello+world");

     

    setRequestHeader 函数

    此函数设置header头,向服务器描述header;头一般用来向服务器说明是用GET还是POST方法

     

    setRequestHeader 函数

    setRequestHeader("string headername", "string headervalue")
    第一个参数表明头的名字
    第二个参数是值
    如果用了POST方法
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


    一个完整的POST提交像这样:

    var request = getHTTPObject();  
    if (request)  
    { request.onreadystatechange = doSomething; 
    request.open("POST", "file.ext", true);  
     request.setRequestHeader("Content-Type", "application/ x-www-form-urlencoded"); 
     request.send("name=Jeremy+Keith&message=Hello+world"); } 
  • 相关阅读:
    Python随笔之字典Dict
    Python随笔之列表List
    Python基础整理,懒得分类了,大家对付看看吧
    shell批量推送公钥脚本
    Xcode 切换target 之后打包失败
    百度导航sdk错误日志
    前端项目中去掉替换双引号
    taro3.x项目中引用taro-ui以及taro-iconfont-cli
    taro
    JS中some() every() forEach() map() filter()的区别
  • 原文地址:https://www.cnblogs.com/mount/p/2251296.html
Copyright © 2011-2022 走看看