一,axios跨域访问PHP后端

1,没有在PHP后端代码中设置跨域访问相关配置项之前。
(1)前端向后端发起ajax请求。

  1. axios.post('http://localhost/smartisanPHP/admin/public.php',data).then(function(result){
  2. console.log(result);
  3. })

(2)后端给前端的响应状态如下

  1. Response to preflight request doesn't pass access control check:
  2. No 'Access-Control-Allow-Origin' header is present on the requested resource.
  3. Origin 'http://localhost:8080' is therefore not allowed access.

(3)表明后端在默认情况下是不允许前端跨域访问的。

2,在PHP后端php文件中新增如下代码,让其允许前端进行跨域访问。

(1)调整后端代码。

  1. header('Access-Control-Allow-Origin:*');
  2. header('Access-Control-Allow-Methods:GET,POST');
  3. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
  4. header('content-type:application/json;charset=utf-8');

(2)重新在前端发起请求,查看结果。

  1. axios.post('http://localhost/smartisanPHP/admin/public.php',data).then(function(result){
  2. console.log(result);
  3. })

(3)后端给前端的响应结果如下:

  1. <br />
  2. <b>Notice</b>: Undefined index: name in <b>C:\phpStudy\PHPTutorial\WWW\smartisanPHP\admin\public.php</b> on line <b>28</b><br />
  3. <br />
  4. <b>Notice</b>: Undefined index: name_intro in <b>C:\phpStudy\PHPTutorial\WWW\smartisanPHP\admin\public.php</b> on line <b>29</b><br />
  5. <br />
  6. <b>Notice</b>: Undefined index: price in <b>C:\phpStudy\PHPTutorial\WWW\smartisanPHP\admin\public.php</b> on line <b>30</b><br />
  7. [] //在后端通过echo json_encode($_POST);输出前端提交的数据包,发现该数据包为空
  8. 数据存储成功

3,后端接收的的$_POST数据之所以为【空数组】,是因为axios默认发送给后端的数据包格式不正确。

(1)调整前端发起ajax请求的代码

  1. var data = new FormData(); //表单对象
  2. data.append('name',this.gData.name);
  3. data.append('name_intro',this.gData.name_intro);
  4. data.append('price',this.gData.price);
  5. axios.post('http://localhost/smartisanPHP/admin/public.php',data).then(function(result){
  6. console.log(result);
  7. })

(2)后端将可以正确接收前端提交的$_POST数据。

二,在Vue根对象的原型下挂载方法

当我们在使用axios的时候,如果不将axios方法对象挂载到Vue对象下使用的话,则我们需要在每个组件里面都要通过import方法去引入axios模块才能使用,这种使用方式不是很方便,我们采用如下流程,将axios方法挂载到Vue对象的原型下。
(1)在项目的main.js中加入如下代码

  1. import axios from 'axios';
  2. Vue.prototype.$axios = axios; //将axios方法模块挂载到Vue对象的原型下面
  3. Vue.prototype.getFormData = function(obj){ //将FormData数据包的处理方法函数挂载到Vue对象的原型下面
  4. var data = new FormData();
  5. for(var attr in obj){
  6. data.append(attr,obj[attr]);
  7. }
  8. return data;
  9. }

(2)在项目的.vue单文件组件中通过this.$axios的方式,直接使用axios。

  1. methods: {
  2. handleTabsAdd () {
  3. this.tabs ++;
  4. },
  5. pubFn(){
  6. var data = this.getFormData(this.gData); //使用Vue.prototype下的getFormData方法
  7. this.$axios.post('http://localhost/smartisanPHP/admin/public.php',data)
  8. //使用Vue.prototype下的$axios方法
  9. .then(function(result){
  10. console.log(result);
  11. })
  12. }
  13. }

三,前端获取数据并展示的流程

(1)MySQL数据库中已经有录入的商品数据。

smartisan商城实战之后端篇 - 图1

(2)新增PHP后端逻辑文件list.php,当用户请求这个接口文件的时候,将数据库中的数据全部获取后,返回给前端。

  1. <?php
  2. header('Access-Control-Allow-Origin:*');
  3. header('Access-Control-Allow-Methods:GET,POST');
  4. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
  5. header('content-type:application/json;charset=utf-8');
  6. if($connect = mysqli_connect('localhost', 'root', 'root')){
  7. if($db = mysqli_select_db($connect, 'smartisan')){
  8. $select = "select * from goods"; //用以查询数据的sql语句
  9. if($result = mysqli_query($connect, $select)){
  10. $gdata = array(); //存储查询得到的所有数据
  11. while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
  12. array_push($gdata,$row);
  13. }
  14. $state=array('code'=>1,'msg'=>'数据存储成功','gdata'=>$gdata);
  15. echo json_encode($state);
  16. }
  17. }
  18. }
  19. ?>

(3)在用户端smartisan-build项目中的Home.vue组件中,使用axios发起ajax请求并获取数据。

  1. export default {
  2. data() {
  3. return {
  4. goodsData:[]
  5. }
  6. },
  7. components:{
  8. "good-item":GoodItem
  9. },
  10. created(){
  11. var _This = this;
  12. var gdata;
  13. this.$axios.get('http://localhost/smartisanPHP/admin/list.php')
  14. .then((result) => {
  15. gdata = result.data.gdata;
  16. var Len = gdata.length;
  17. for(var i=0;i<Len;i++){
  18. gdata[i].stock_info = JSON.parse(gdata[i].stock_info); //将字符串格式的数据转换为json对象
  19. }
  20. _This.goodsData = gdata
  21. }).catch((err) => {
  22. });
  23. }
  24. }

(4)将获取到的数据goodsData绑定至组件的HTML结构中显示。

  1. <div class="item-box">
  2. <good-item
  3. v-for="(item,index) in goodsData"
  4. v-bind:key="index"
  5. :g-data="item"
  6. :g-index="item.id">
  7. </good-item>
  8. </div>

四,后端数据库及代码线上部署

1,将本地MySQL数据表导出并上传至百度云虚机的MySQL数据库。
(1)本地导出sql数据表。
smartisan商城实战之后端篇 - 图2
(2)打开百度云MySQL数据库。
【主机控制台】—【控制面板】—【数据库管理】
smartisan商城实战之后端篇 - 图3

(3)在百度云MySQL数据库中导入第一步中得到的goods.sql文件
smartisan商城实战之后端篇 - 图4

2,迁移PHP后端逻辑代码。
(1)得到百度云数据库相关信息。
【主机控制台】—【控制面板】—【查看更多主机信息】
smartisan商城实战之后端篇 - 图5
(2)将原来放在本地phpstudy服务器的www目录下的所有项目相关的.php文件中的数据库连接信息,改为百度云数据库的信息。以list.php文件代码为例:

  1. <?php
  2. header('Access-Control-Allow-Origin:*');
  3. header('Access-Control-Allow-Methods:GET,POST');
  4. header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
  5. header('content-type:application/json;charset=utf-8');
  6. if($connect = mysqli_connect('sqld-gz.bcehost.com:3306', '0fba361d6c4d4996957f4f6941d5510b', '上图中的MySQL密码')){
  7. if($db = mysqli_select_db($connect, 'WOoDEPxRwDxuwHjxkpKT')){
  8. $select = "select * from goods";
  9. if($result = mysqli_query($connect, $select)){
  10. $gdata = array();
  11. while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
  12. array_push($gdata,$row);
  13. }
  14. $state=array('code'=>1,'msg'=>'数据存储成功','gdata'=>$gdata);
  15. echo json_encode($state);
  16. }
  17. }
  18. }
  19. ?>

(3)通过FTP工具(winscp)连接云虚机,在其webroot目录下新建一个跟本地路径一样的smartisanPHP文件夹,并将所有.php文件上传至该文件夹中。
smartisan商城实战之后端篇 - 图6

3,迁移【iview后端管理界面】代码至云虚机。
因为云虚机的webroot根目录下,已经放置了用户端代码打包后的static文件夹及index.html,所以我们需要在webroot中新建一个admin文件夹,将【iview后端管理界面】打包代码,放在该admin文件中。这会导致打包后的index.html文件中引入js与css默认路径无法匹配,需要调整打包配置项。
(1)在myapp目录下的config文件夹中的index.js中调整打包配置项。

  1. build: {
  2. // Template for index.html
  3. index: path.resolve(__dirname, '../dist/index.html'),
  4. // Paths
  5. assetsRoot: path.resolve(__dirname, '../dist'),
  6. assetsSubDirectory: 'static',
  7. assetsPublicPath: '/admin/', //修改打包后html页面引用js及css的路径配置,默认为 '/'
  8. }

(2)将myapp项目中的所有axios发起的请求路径,改为线上路径,示例如下:

  1. del(i) {
  2. var _This = this;
  3. var id = this.data1[i].id;
  4. var data = this.getFormData({gid:id})
  5. this.$axios.post('http://wenfeiwenfei.gz01.bdysite.com/smartisanPHP/admin/del.php',data)
  6. .then(function(result){
  7. if(result.data.code==1){
  8. _This.$Message.info('删除成功');
  9. _This.data1.splice(i,1);
  10. }
  11. })
  12. }

(3)执行如下命令,打包iview后端界面

  1. npm run build

(4)将打包得到的dist目录下的所有文件,上传至云虚机的webroot下的admin文件中。
smartisan商城实战之后端篇 - 图7

(5)访问线上的iview后端界面,并测试其功能。

  1. http://wenfeiwenfei.gz01.bdysite.com/admin

(6)如果迁移成功,线上的iview界面功能应该跟本地测试功能一样。