vue 跨域访问http

axios用法:

npm install axios --save-dev

2.导入:

import axios from 'axios';

3.使用($(form)需要先按装jQuery)

axios.post('http://localhost:3000/users/zhuce',$(form).serialize())
    .then(function(res){
        console.log(res);
        alert(res.data);
    })
    .catch(function(err){
        console.log(err);
    })

4.$(form)需要先按装jQuery

npm install  jquery --save-dev 

5.到build/webpack.base.conf.js中,

导入:var webpack = require('webpack');
最下面添加:

plugins: [  
    new webpack.ProvidePlugin({  
         $:"jquery",  
         jQuery:"jquery",  
         "window.jQuery":"jquery"  
      })  
    ] 

vue文件中无需导入,可直接使用$和jQuery

1.安装

npm install vue-resource --save-dev 

2.main.js中

Vue.use(require('vue-resource'));  //--------添加

3.vue中使用

callKa:function(event){
      this.$http.get('http://localhost:3000/',{}).then(function(data){
        alert(data.body);
      }).catch(function(err){
          alert('出错');
          for(let key in err){
            console.log(key+':'+err[key]);
          }
      })
    }

 

转载于:https://www.cnblogs.com/yu-hailong/p/8404517.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值