经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue进行post和get请求实例讲解
来源:jb51  时间:2022/3/8 10:43:58  对本文有异议

使用axios:

  1. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  2.  

一、基本使用方法

1、get请求

  1. // Make a request for a user with a given ID
  2. axios.get('/user?ID=12345')
  3. ?.then(function (response) {
  4. ? console.log(response);
  5. ?})
  6. ?.catch(function (error) {
  7. ? console.log(error);
  8. ?});
  9. ?
  10. // Optionally the request above could also be done as
  11. axios.get('/user', {
  12. ? params: {
  13. ? ?ID: 12345
  14. ? }
  15. ?})
  16. ?.then(function (response) {
  17. ? console.log(response);
  18. ?})
  19. ?.catch(function (error) {
  20. ? console.log(error);
  21. ?});

2.Post请求

  1. axios.post('/user', {
  2. ?firstName: 'Fred',
  3. ?lastName: 'Flintstone'
  4. })
  5. .then(function (response) {
  6. ?console.log(response);
  7. })
  8. .catch(function (error) {
  9. ?console.log(error);
  10. });

 简单示例:

  1. // 在进行 post 和 get 请求的时候,使用 axios 进行访问
  2. // 进行 get 请求
  3. axios.get(url).then(function (response){
  4. ? ? console.log(response);
  5. }).catch(function(error){
  6. ? ? console.log(error);
  7. });
  8. // 进行post 请求 ? ? ? ? ? ?
  9. axios.post(url,{firstName:'Fred',lastName:'Flintstone'}).then(function (response) {
  10. ? ? console.log(response);
  11. }).catch(function (error) {
  12. ? ? console.log(error);
  13. });

 这样发送请求,虽然是可以发送,但是携带的参数,是一个json字符串,会出现问题。所以我们在用post发送请求的时候,需要这样:

  1. axios({ ?
  2. ? ? method:'post', ?
  3. ? ? url:url, ?
  4. ? ? data:{title:this.title,content:this.content}, ?
  5. ? ? headers:{'Content-Type': 'application/x-www-form-urlencoded'}, ?
  6. ? ? transformRequest: function(obj) { ?
  7. ? ? ? ? var str = []; ?
  8. ? ? ? ? for(var p in obj){ ?
  9. ? ? ? ? ? ? str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); ?
  10. ? ? ? ? } ?
  11. ? ? ? ? return str.join("&"); ?
  12. ? ? } ?
  13. }).then((res)=>{
  14. ? ? console.log(res.data);
  15. });

上面这种只能提交一些简单的数据,对于复杂的数据,可以考虑使用 QS 对数据进行处理。

使用方法,如果找不到QS文件,可以只用 npm 安装:

npm install qs

下载这个文件之后,使用 script 标签正常引入即可。

使用方法:

  1. var formData = Qs.stringify({imgIds: [48,49],});
  2. axios.post(url,Qs.stringify(this.formData)).then(function (response) {
  3. ? ? console.log(response);
  4. }).catch(function (error) {
  5. ? ? console.log(error);
  6. });

或者是:

  1. axios({
  2. ? ? method: 'post',
  3. ? ? url:url,
  4. ? ? data:Qs.stringify(this.formData),
  5. }).then((res)=>{
  6. ? ? console.log(res);
  7. });

到此这篇关于vue进行post和get请求实例讲解的文章就介绍到这了,更多相关vue进行post和get请求内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号