美文网首页
ajex有参数请求

ajex有参数请求

作者: 黄稚女 | 来源:发表于2019-02-05 22:25 被阅读0次
  1. 创建form表单,给用户输入信息
  2. 获取提交按钮,并添加点击的时间
  3. 给予提交按钮一个回调函数,并用return false语句禁止提交按钮的默认行为
    创建一个Ajax对象
    创建请求监听并判断
  4. 获取用户的参数来构建一个uri地址
  5. 请求方式用get

ajax.html代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="test1.php" method="get">
        <input type="text" name="username" id = 'username'>
        <input type="password" name="password" id = 'password'>
        <input type="submit" name="" id = 'submit'>
    </form>
</body>
<script>
    var btn = document.getElementById('submit');
    btn.onclick = function(){
        console.log('点击事件成功<br>');    
        var xhr = new XMLHttpRequest(); 
        xhr.onreadystatechange = function () { 
                console.log('监听到状态变化'+xhr.readyState+'<br>'); 
             if (xhr.readyState == 4 && xhr.status == 200){
                console.log('发送成功 接收成功<br>');

                document.write(xhr.responseText);

             }
        }
         var username = document.getElementById('username').value;          // 获取input里得值
         //console.log(username);
         var password = document.getElementById('password').value;
         var uri = 'test1.php?username='+username+'&password'+password;     // 拼接uri请求得地址

         document.write(uri);

        xhr.open('get',uri,true);                                       // 通过test.php?user=?? & pass=xxx发送数据
        xhr.send(null); 
        return false; 
    }
     
</script>
</html>

test.php代码如下

<?php 
echo "ggg";
 ?>

相关文章

  • ajex有参数请求

    创建form表单,给用户输入信息 获取提交按钮,并添加点击的时间 给予提交按钮一个回调函数,并用return fa...

  • Python的requests模块

    无参数的get请求 有参数的get请求 POST请求 使用Session

  • Gin-获取POST请求参数

    有默认值方式获取POST请求参数 无默认值方式获取POST请求参数 获取POST请求参数的同时,返回参数获取状态,...

  • GET和POST的区别

    POST请求和GET请求都可以传递参数。POST请求有body,参数数据放在body中。而GET请求参数数据放在h...

  • Gin-获取GET请求参数

    有默认值方式获取GET请求参数 无默认值方式获取GET请求参数 获取GET请求参数的同时,返回参数获取状态,状态为...

  • Python requests传参方式

    GET请求 无参数 有参数 POST请求 表单提交 x-www-form-urlencoded形式增加header...

  • controller方法

    1. controller方法请求参数 1.1 请求参数 1.2 请求参数部分详解 1.2.1 @MatrixVa...

  • Axios&fetch

    axios 安装 请求 参数 fetch 安装 请求 参数

  • day10 以文章的为例的接口文档

    文章查询接口api 请求URL: 请求方式: 请求参数: 响应: 响应参数:

  • Get与Post的请求响应的差异

    get请求的参数放在url中,即在请求头中可见。不能携带大量参数。 post请求的参数在请求正文中。可以携带大量参数。

网友评论

      本文标题:ajex有参数请求

      本文链接:https://www.haomeiwen.com/subject/hfkesqtx.html