【问题标题】:'Access-Control-Allow-Origin' error in MeteorJSMeteorJS 中的“访问控制允许来源”错误
【发布时间】:2017-03-23 06:21:58
【问题描述】:

我正在使用 Laravel Lumen 为我的 MeteorJS 应用程序创建一个 API。这是我在 imports\api\tasks.js 上的代码 inm y meteorJS

...
import { HTTP } from 'meteor/http';
import { WebApp } from 'meteor/webapp';

if (Meteor.is_client) {

    // Calling our Meteor server's function 
    // and simply storing data into current session
    Meteor.call('fetchDataFromUrl', function (error, response) {
        Session.set('external_server_data', response)
    });


    // Providing meteor data for template (it renders on data received)
    Template.data.server_data = function () {
        return Session.get('external_server_data');
    };

}

if (Meteor.is_server) {

    Meteor.methods({
        // Declaring a method
        retrieve_doc_types: function () {
            this.unblock();
            return Meteor.http.get(api_url);
        }
    });

}

Meteor.methods({

  'tasks.insert'(make, model, year) {
    check(make, String);
    check(model, String);
    check(year, String);

    if (! Meteor.userId()) {
      throw new Meteor.Error('not-authorized');
    }

    HTTP.call("POST", "http://localhost:8000/api/v1/car",
          {data: {"make":make, "model":model, "year":year}},
        function (error, result) {

          if (!error) {
              console.log(result);
          } else{

              console.log("http post error");
          };
        });
  },

....

但是当我得到这个错误时:

XMLHttpRequest cannot load http://localhost:8000/api/v1/car. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.
tasks.js:81 http post error

有人有想法吗?我是 MeteorJS 的新手

【问题讨论】:

    标签: javascript api meteor meteor-blaze restful-url


    【解决方案1】:

    在你的 server/main.js 中试试这个

    WebApp.rawConnectHandlers.use(function(req, res, next) {
      res.setHeader("Access-Control-Allow-Origin", "*");
      return next();
    });
    

    【讨论】:

    • 你在使用 nginx 吗?
    • 我正在使用笔记本电脑,我正在使用 XAMPP,Windows 10 操作系统
    【解决方案2】:

    你在哪里调用方法?该方法称为tasks.insert,但您提供的代码仅调用fetchDataFromUrl 方法。

    这里有一些想法。

    • 检查您在客户端上的调用是异步使用的。来自 Metor HTTP 文档:On the client, this function must be used asynchronously by passing a callback. Note that some browsers first send an OPTIONS request before sending your request (in order to determine CORS headers).

    • 我的一个项目中也遇到了 CORS 问题,最后我只使用了 HTTP 库服务器端。你可以用 Meteor.isServer 包围你的 HTTP 调用。

    【讨论】:

      【解决方案3】:

      有人试图回答但我不明白。你试试运气。 https://codexample.org/questions/9358/no-access-control-allow-origin-error-in-meteor-app.c

           Try package - simple:json-routes and put following code at serverside startup.
          // Enable cross origin requests for all endpoints
          JsonRoutes.setResponseHeaders({
            "Cache-Control": "no-store",
            "Pragma": "no-cache",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET, PUT, POST, DELETE, OPTIONS",
            "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With"
          });
      

      【讨论】:

        猜你喜欢
        • 2013-03-02
        • 1970-01-01
        • 2017-01-10
        • 2017-11-21
        • 2019-07-09
        相关资源
        最近更新 更多