zoukankan      html  css  js  c++  java
  • 原生js发送请求

    const loginForm = document.querySelector('#loginForm');
    const loginName = document.querySelector('#loginName');
    const loginPwd = document.querySelector('#loginPwd');
    
    loginForm.addEventListener('submit', function (e) {
            e.preventDefault();
            const name = loginName.value;
            const pwd = loginPwd.value;
            const xhr = new XMLHttpRequest();
    
            xhr.addEventListener('load', function () {
                const data = JSON.parse(xhr.responseText);
                if (data.code === 1) {
                    location.href = '/';
                }
                else {
                    alert(data.message);
                }
            });
    
            xhr.open('POST', '/mide/student/api/login');
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify({
                loginName: name,
                loginPwd: pwd
            }));
        })

    此为登录HTML请求

    form表单submit 自带发送请求事件

    e.preventDefault();

    在事件中加入   可 阻止此事件

  • 相关阅读:
    docker 安装 redis
    docker 安装 fastdfs
    docker 安装 mysql5.7
    docker 安装 nacos
    docker 安装 gitlab-ce
    gitlab记录
    git记录
    ubuntu命令
    java html table 转 excel,给予jdom 和 poi
    自律挑战
  • 原文地址:https://www.cnblogs.com/eunuch/p/9776298.html
Copyright © 2011-2022 走看看