首页 > 其他分享 >AJAX&&Axios

AJAX&&Axios

时间:2022-09-22 21:56:40浏览次数:57  
标签:function Axios http demo resp xhttp AJAX && localhost

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

 

<head> <script> function loadXMLDoc() { .... AJAX 脚本执行 ... } </script> </head>

步骤:

    //1. 创建核心对象
    var xhttp;
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //2. 发送请求
    xhttp.open("GET", "http://localhost:8080/ajax-demo/ajaxServlet");
    xhttp.send();

    //3. 获取响应
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
               alert(this.responseText);
        }
    };

Axios是对AJAX的一个封装,在使用之前需要声明需要导入的js文件

<script src="js/axios-0.18.0.js"></script>
使用步骤:
<script>
    //1. get
   /* axios({
        method:"get",
        url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
    }).then(function (resp) {
        alert(resp.data);
    })*/


    //2. post
    axios({
        method:"post",
        url:"http://localhost:8080/ajax-demo/axiosServlet",
        data:"username=zhangsan"
    }).then(function (resp) {
        alert(resp.data);
    })



</script>

 或者使用别名的方式:

    axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan").then(function (resp) {
        alert(resp.data);
    })

 axios.post("http://localhost:8080/ajax-demo/axiosServlet","username=zhangsan").then(function (resp) {
        alert(resp.data);
    })

 

标签:function,Axios,http,demo,resp,xhttp,AJAX,&&,localhost
From: https://www.cnblogs.com/zhaolei0419/p/16720987.html

相关文章

  • vue学习笔记(三):axios获得远程数据,拦截器
     安装:npmiaxios 请求数据代码如下:<script>importaxiosfrom'axios';exportdefault{data:()=>{return{name:''},methods:{set_val(){......
  • jQuery中ajaxPrefilter的应用[设置通用参数]
    $.ajaxPrefilter(function(options,originalOptions,jqXHR){//tm1、tm2表示开始时间、结束时间varrequestType,params,startTime,endTime;requestType=(......
  • axios实现实时获取文件上传/下载进度
    <template><div><button@click="downLoad">下载</button><p>下载进度:{{downLoadProgress}}</p></div></template><script......
  • Ajax
    使用ajax进行新增<script>$(function(){$("#Add").click(function(){$.ajax({type:"Post",url:"/UserLogin/AddUser",data:{Name:$("#txtName").val(),Age:$("#t......
  • Ajax
    使用ajax进行新增<script>$(function(){$("#Add").click(function(){$.ajax({type:"Post",......
  • Node.js(七)MySql+ajax
    Api.jsconstexpress=require("express");constrouter=express.Router();constmysql=require('mysql')constmysqlutil=require("./mysql");letpool=mysql.cre......
  • nodejs抓取接口数据(axios)
     demo:varaxios=require("axios");varfs=require("fs");varnodeschedule=require("node-schedule");varhotListUrl="https://weibo.com/ajax/side/hotS......
  • 安装eslint后可能因为eslint版本过高导致axios安装失败
    安装eslint后可能因为eslint版本过高导致axios安装失败,也就是包冲突有两个命令可以解决此问题一是--force无视冲突,强制获取远端npm库资源(覆盖之前)二是--legacy-pee......
  • ajax+tp5 详情页跳转/实现登录--注册
      需求:点击资料下载后   跳转到登录页登录成功下载资料,  有账号即可登录  无账号即需注册!<formaction=""method="POST"role="form"class="logi......
  • 如何在 React 中进行 Axios POST 请求?
    如何在React中进行AxiosPOST请求?我们将制作一个AxiosPOST请求创建数据或将数据插入数据库。我们将在POST请求中发送请求参数,并且还将举例发送HTTP标头。在......