【问题标题】:Pencilblue - origin not allowed for controller endpont铅笔蓝 - 控制器端点不允许原点
【发布时间】:2015-10-30 12:35:53
【问题描述】:

我正在使用 Pencilblue 开发 API,在我向该端点发出 AJAX 请求之前一切正常。

我总是收到“来源不允许”错误。

有没有办法用pencilblue启用CORS?

【问题讨论】:

    标签: ajax cors pencilblue


    【解决方案1】:

    您可以通过创建自己的控制器来做到这一点:

    https://github.com/pencilblue/pencilblue/wiki/Quickstart:-Controllers

    并在其中添加 CORS 的相关标头。例如。类似:

    module.exports = function (pb) {
    
        //PB dependencies
        var util           = pb.util;
        var BaseController = pb.BaseController;
    
        /**
         * CORS Controller
         */
        CorsController.prototype.render = function(cb) {
    
        // Add the CORS Header here
        var output = {
            code: 200,
            headers: {
                  'Access-Control-Allow-Origin': '*'
            }            
        };
        this.ts.load('example_api_endpoint', function(error, result) {
            output.content = result;
            cb(output);
        });
    
       return CorsController;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2020-08-14
      • 2015-05-18
      • 2016-09-08
      • 2022-12-21
      • 2020-05-28
      相关资源
      最近更新 更多