【问题标题】:Stripe custom typography font条纹自定义排版字体
【发布时间】:2018-08-08 07:14:38
【问题描述】:

我的网络服务器上托管了一个 cloud.typography 字体。当我尝试使用stripe.elements() 将其链接到条带时,我的字体没有显示,并且默认为 Helvetica。我也没有收到任何 javascript 错误。

这是我的 javascript 代码:

var elements = stripe.elements({
    fonts: [
        {
            cssSrc: 'localhost:8080/static/fonts/######/#################.css',
        },
    ],
});
var baseStyle = {
    fontFamily: 'Gotham Narrow A, Gotham Narrow B, sans-serif',
    fontSize: '16px',
    lineHeight: '1.5',
    fontWeight: '300',
    color: '#2d3138',
    '::placeholder': {
        color: '#8f95a3'
    }
};
var invalidStyle = {
    fontFamily: 'Gotham Narrow A',
    fontSize: '16px',
    lineHeight: '1.5',
    fontWeight: '300',
    color: '#e70000'
};
var style = {
    base: baseStyle,
    invalid: invalidStyle
};
var styles = {style: style};
var card = elements.create('cardNumber', styles);

如果我尝试使用它给我的 css 链接直接链接到排版,我会收到以下错误:

Failed to load https://cloud.typography.com/#######/#######/css/fonts.css: 
Redirect from 'https://cloud.typography.com/#######/#######/css/fonts.css' 
to 'https://myhostedwebsite.com/fonts/######/#################.css' 
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 
header is present on the requested resource. Origin 'http://localhost:8080' 
is therefore not allowed access.

我也尝试将我的字体作为字体系列对象导入。

var elements = stripe.elements({
    fonts: [
        {
            family: 'Gotham Narrow A',
            src: 'url(https://myhostedwebsite.com/fonts/######/#################.eot)',
            style: 'normal',
            weight: '300',
        },
    ],
});

请帮忙。

【问题讨论】:

  • 您使用的是哪种浏览器?
  • 最新版谷歌浏览器

标签: javascript stripe-payments webfonts cloud.typography


【解决方案1】:

编辑

@Michael 发现他必须将 stripe.com 添加到 cloud.typography 项目仪表板上的已启用域列表中。现在条纹元素正在使用 cloud.typography 字体。这在使用 --disable-web-security 标志运行 chrome 时有效。但是,当使用 chrome 扩展时,这些结果不起作用。


你的错误说:

Failed to load https://cloud.typography.com/#######/#######/css/fonts.css: 
Redirect from 'https://cloud.typography.com/#######/#######/css/fonts.css' 
to 'https://myhostedwebsite.com/fonts/######/#################.css' 
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 
header is present on the requested resource. Origin 'http://localhost:8080' 
is therefore not allowed access.

Chrome 不支持 localhost 的 CORS 请求。

您可以使用Allow-Control-Allow-Origin: * Chrome 扩展程序来解决此问题。该扩展将为 CORS 添加必要的 HTTP 标头。

使用 --disable-web-security 标志启动 chrome

【讨论】:

  • 我使用 --disable-web-security 标志运行 google chrome 并测试了我的网站。但是,我的字体仍然没有出现。来自控制台的错误是这样的:Failed to load resource: the server responded with a status of 403 (Forbidden).
  • 你试过 chrome 扩展吗?
  • 我也尝试过 chrome 扩展。我收到了这个错误:The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https://m.stripe.network' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
  • 哦,好吧,我搞定了。这就是我所做的。我必须将 stripe.com 添加到 cloud.typography 项目仪表板上的启用域列表中。现在我的条纹元素正在使用我的 cloud.typography 字体。当我使用 --disable-web-security 标志运行 chrome 时,这是有效的。但是,使用 chrome 扩展时我无法得到这些结果。
  • @Michael 我只是在研究这个。我会为社区相应地更新答案。
【解决方案2】:

这是一个示例,演示如何从第三方页面(来自您不一定控制的另一个域)加载字体。

https://jsfiddle.net/5yz4z2yn/104/

您还必须在 CSS 中导入 webfont。

@import url('https://fonts.googleapis.com/css?family=Indie+Flower');

【讨论】:

  • 此答案不适用于我的问题,因为 cloud.typography 是基于订阅的网络字体。我不能简单地使用 url 参数访问字体。 cloud.typography 用来启用我购买的字体的过程是这样的: Cloud.typography 为我的 webfonts 生成十六进制代码并创建 .css.eot 文件。然后我下载这些字体并将它们移动到我的网站目录中。然后当我发布我的网站时,字体安装由 cloud.typography 确认。一旦字体被确认,它们就会被激活。 .css 文件包含所有 @font-face{} 定义。
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 2018-03-31
  • 2021-04-24
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多