showdoc深度二次开发

vue开发模式下的API访问跨域问题

小韦云科技-区块链+小程序+公众号+商城+分销+直播+企业官网+外贸电商-为您提供优质的开发服务-电话/微信联系:18123611282

进入ShowDoc前端开发模式后,会发现有API跨域问题,导致后台数据请求失败
在cmd中显示的报销

[HPM] Error occurred while trying to proxy request /server/index.php?s=/api/common/homePageSetting from localhost:8080 to http://127.0.0.1/showdoc/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /server/index.php?s=/api/user/info from localhost:8080 to http://127.0.0.1/showdoc/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /server/index.php?s=/api/ScriptCron/run from localhost:8080 to http://127.0.0.1/showdoc/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

在浏览器上显示

打开web_src/index.html文件,由于我本地的PHP运行根目录已经配置到showdoc根目录下,直接通过http://127.0.0.1/server/index.php 可以访问到showdoc的后台PHP系统。因此把下面的代码:

    window.DocConfig = {
      //"server":'http://127.0.0.1/showdoc/server/index.php?s=',
      "server": window.location.protocol +'//'+ window.location.host + window.location.pathname+ '../server/index.php?s=',
      //"lang" :'en'
      "lang" :'zh-cn'
    }

修改为

    window.DocConfig = {
      "server":'http://127.0.0.1/server/index.php?s=',
      //"server": window.location.protocol +'//'+ window.location.host + window.location.pathname+ '../server/index.php?s=',
      //"lang" :'en'
      "lang" :'zh-cn'
    }

查看了下代码,发布showdoc已经有使用axios来解决这个跨域问题的,如:

that.axios.post(url, params)

那问题应该出在后台API在,后台需要配置cors,打开server/Application/Api/Controller/BaseController.class.php 文件,找到以下代码

        //是否开启本地调试。
        $this->is_local_debug = 0 ;

把它修改为1,表示打开调试

        //是否开启本地调试。
        $this->is_local_debug = 1 ;

因为它下面的返回方法就是通过这个参数来控制是否需要打开cors的

        if ($this->is_local_debug > 0 ) {
            header('Access-Control-Allow-Origin: *');//允许跨域请求
            header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie');
            header('Access-Control-Allow-Credentials: true');//允许跨域请求
        }

就这样跨域问题就可以解决了

本文由小韦云原创,转载请注明出处:https://www.bctos.cn/doc/9/1815,否则追究其法律责任

关键词:showdoc前端开发,vue开发,API跨域,axios,打开调试

广告位招商