vue 的爬坑之路(一)--跨域 原: https://blog.csdn.net/y_z_w123/article/details/80251255

前提使用脚手架安装的情况

第一种本地使用代理的情况

在confin/index.js   13行
proxyTable: {
      '/api': {
        target: '你自己的接口地址',
        changeOrigin:true,//实行跨域
        pathRewrite: {
          '^/api': ' '
        }
      }
    },
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

项目的使用 axios本身支持跨域

在src/main.js
import axios from 'axios'
Vue.prototype.$axios = axios

是在你的vue文件内其他请求请看axios的安装和使用教程
var formData = JSON.stringify(this.commons);
console.log(formData)
this.$axios.post('api/commons' ,formData ,{
  headers: {'Content-Type':'application/x-www-form-urlencoded'}
})
  .then(function (response) {
    console.log(response)
      if (response.status === 201){
        alert('感谢您的来稿,你的建议稍后呈现')
        window.location.reload()
      }
  })
  .catch(function (error) {
    console.log(error)
  });
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
formDate是你要传递的数据。header头一定要添加。其中用到了axios.

 
 
  • 1

axios的安装和使用教程

https://github.com/axios/axios
 
 
  • 1

打包

npm run build
 
 
  • 1

注意:只是本地可以跑,放在线上的话自行百度如何处理(本人目前没有爬出,服务器是Apache的。有搞定的话请分享下)

第二种不使用代理的情况

请先按第一种的方法进行安装和配置
区别

qs的安装

npm install qs
 
 
  • 1

项目中的使用

在src/main.js   
import Qs from 'qs'
Vue.use(Qs)

项目中的文件
 const Qs = require('qs')
 var formData = Qs.stringify(this.commons);
 console.log(formData)
 this.$axios.post('你接口数据地址' ,formData ,{
   headers: {'Content-Type':'application/x-www-form-urlencoded'}
 })
   .then(function (response) {
     console.log(response)
       if (response.status === 201){
         alert('感谢您的来稿,你的建议稍后呈现')
         window.location.reload()
       }
   })
   .catch(function (error) {
     console.log(error)
   });
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

后端的配合(本人后端是PHP用的yii2框架)

public function behaviors()
{
   return ArrayHelper::merge([
       'contentNegotiator' => [
           'class' => ContentNegotiator::className(),
           'formats' => [
               'text/html' => Response::FORMAT_JSON,
               'application/json' => Response::FORMAT_JSON
           ]
       ],
       [
           'class' => Cors::className(),
           'cors' => [
               'Origin' => ['*'],
               'Access-Control-Allow-Methods' => ['POST', 'GET', 'PUT', 'DELETE', 'OPTIONS'],
               'Access-Control-Allow-Headers' => ['x-request-with,content-type'],
               'Access-Control-Max-Age' => 86400

           ],

       ],
   ],parent::behaviors()); // TODO: Change the autogenerated stub
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

摘要

简明下JSON和qs插件的区别
JSON:{"id":1,"name":tom}
qs id=1&name=tom

第二种方法放到线上
1.打包 npm run build
2.直接配置虚拟主机:域名和指定到你放的项目路径

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值