当前位置: 首页 > news >正文

旅游网站开发网站营销与推广

旅游网站开发,网站营销与推广,网店推广有哪些新思维,在线制作海报网站背景&#xff1a;我在前端使用vue语言开发的&#xff0c;请求的后端是用ThinkPhp项目开发的。我vue项目里的请求php接口&#xff0c;自带header参数的跨域问题通过网上查询到的server端配置方法已经解决了。我使用的 是中间件的配置方法&#xff1a; <?php//admin 项目 配…

背景:我在前端使用vue语言开发的,请求的后端是用ThinkPhp项目开发的。我vue项目里的请求php接口,自带header参数的跨域问题通过网上查询到的server端配置方法已经解决了。我使用的

是中间件的配置方法:

<?php//admin 项目 配置中间件
use app\admin\middleware\MyCrossDomain;return [MyCrossDomain::class
]; 

 MyCrossDomain.php

<?php
namespace app\admin\middleware;use Closure;
use think\Config;
use think\Request;
use think\Response;/*** 跨域请求支持*/
class MyCrossDomain
{protected $cookieDomain;protected $header = ['Access-Control-Allow-Credentials' => 'true','Access-Control-Max-Age'           => 1800,'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE, OPTIONS','Access-Control-Allow-Headers'     => 'Authorization, Code,Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',];public function __construct(Config $config){$this->cookieDomain = $config->get('cookie.domain', '');}/*** 允许跨域请求* @access public* @param Request $request* @param Closure $next* @param array   $header* @return Response*/public function handle(Request $request, Closure $next, array $header = []): Response{$header = !empty($header) ? array_merge($this->header, $header) : $this->header;if (!isset($header['Access-Control-Allow-Origin'])) {$origin = $request->header('origin');if ($origin && ('' == $this->cookieDomain || str_contains($origin, $this->cookieDomain))) {$header['Access-Control-Allow-Origin'] = $origin;} else {$header['Access-Control-Allow-Origin'] = '*';}}return $next($request)->header($header);}
}

这样之后前端项目通过axios接口请求不再报跨域的错了,这一阶段的问题已经解决。

现在有一个新的问题,我在Thinkphp项目里有一个静态的json文件,H5要通过<link>的方式要请求它。类似这样的:

   document.write("<link rel='manifest' href='https://landpage-server.appboost.co/admin/file/xxxxx/xxxx.json');

虽然我的ThinkPhp项目已经配置了跨域的,但是对于请求这个静态文件还是报跨域的问题。折腾了好久,各种配置还是不行。最后我尝试了一种办法,就是专门写一个Controller来响应静态文件的返回。代码如下:

StaticResourceController.php

<?phpnamespace app\admin\controller;use app\admin\model\LogEvents;
use app\admin\model\PixelInfo;
use app\admin\model\ReleasePlatform;
use app\admin\model\ReleaseUrl;
use app\admin\model\User;
use app\admin\model\Wallet;
use app\admin\model\WalletLog;
use app\BaseController;
use app\Response;
use Ramsey\Uuid\Uuid;
use think\facade\Console;
use think\facade\Db;class StaticResourceController extends MBaseController{public function getManifestJson(){$company_code = input("get.code");$promote_code = input("get.id");// echo root_path();$root =  root_path();$dir = $root ."public/page/";// echo $dir;$content = $this->openFile($dir,$company_code,$promote_code);$obj = json_decode($content);return json($obj, 200);}public function openFile($dir, $company_code,$promote_code){$filePath = $dir.$company_code."/".$promote_code.".json"; // 文件路径$mode = 'r'; // 打开模式$fileHandle = fopen($filePath, $mode);$content = fread($fileHandle, filesize($filePath));if ($fileHandle === false) {die('无法打开文件');}fclose($fileHandle);return $content;}
}

H5端请求是这样的:

   document.write("<link rel='manifest' href='https://landpage-server.appboost.co/admin/staticResource/getManifestJson?code="+$company_code+"&id="+$promote_code+"'>");

谢天谢地,终于不报跨域的问题了,成功拿到了json静态文件。

http://www.yidumall.com/news/49741.html

相关文章:

  • 网站seo课程太原百度网站快速优化
  • 大牌网站设计seo下拉优化
  • 私人pk赛车网站怎么做百度教育官网登录入口
  • 河北建设信息网站合肥seo优化排名公司
  • 网页游戏排行榜gm成都自动seo
  • asp 网站 模板今日最新的新闻
  • 网络科技公司图片seo 优化技术难度大吗
  • 做网站怎么投放广告如何自己做推广
  • ppt的网站导航栏怎么做最新天气预报最新消息
  • 衡水网站开发广州疫情最新消息
  • 用php做企业网站的可行性网站开发流程的8个步骤
  • 网站过期查询seo关键词优化举例
  • 介绍一个做美食的网站长沙seo排名收费
  • 怎么对b2b网站做优化贵阳百度推广电话
  • 芮城网站开发网页制作html代码
  • 西宁网站运营公司注册查询网站
  • 最缺工的一百个职业揭阳seo推广公司
  • 健身网站开发方式郑州百度网站优化排名
  • 网站未建设完善是什么意思新的数据新闻
  • 网站开发需要什么最新百度关键词排名
  • wordpress能导出网站吗在线培训系统app
  • 网站后台系统自媒体视频剪辑培训班
  • 现在网站优化怎么做seo实训报告
  • 网站建设公司利润怎么样百度网站提交
  • 企业网站推广 知乎有哪些实用的网络推广方法
  • 偷拍做自拍视频网站百度seo多久能优化关键词
  • 百度竞价排名又叫seo专家招聘
  • ps怎么网站首页谁能给我个网址
  • 山东网络优化公司排名seo网址超级外链工具
  • 动态手机网站怎么做的南昌seo网站管理