axios+跨域-笔记

本文介绍了axios网络请求库的使用,包括GET和POST请求,并详细阐述了同源策略及其原因。接着探讨了跨域问题,讲解了JSONP和CORS两种解决跨域的方法,其中JSONP适用于低版本浏览器,而CORS是现代浏览器的跨域标准解决方案。
摘要由CSDN通过智能技术生成

axios

http://www.axios-js.com/

Axios是专注于网络数据请求的库

相比于原生的XMLHttpRequest 对象,axios简单易用

相比于jQuery ,axios更加轻量化,只专注于网络请求

axios发起GET请求
// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 上面的请求也可以这样做
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
axios发起POST请求
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
直接使用axios发起请求
// 发送 POST 请求
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});

例子:

document.querySelector("#btn1").addEventListener('click',function(){
   
    var url = 'http://www.liulongbin.top:3006/api/get';
    var paramsObj = {
   name: "zs" , age: 12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值