简体   繁体   English

MeteorJS中的“ Access-Control-Allow-Origin”错误

[英]'Access-Control-Allow-Origin' error in MeteorJS

I am using Laravel Lumen to create an API for my MeteorJS App. 我正在使用Laravel Lumen为我的MeteorJS应用创建API。 This is my code inm y meteorJS on imports\\api\\tasks.js 这是我在imports\\api\\tasks.js上的代码和imports\\api\\tasks.js

...
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");
          };
        });
  },

....

but when I got this 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

do anyone have an idea? 有人有主意吗? I am new with MeteorJS 我是MeteorJS的新手

try this in your server/main.js 在您的server / main.js中尝试一下

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

Where do you call the Method? 您在哪里调用方法? The method is called tasks.insert but the code you provide only calls fetchDataFromUrl method. 该方法称为tasks.insert但您提供的代码仅调用fetchDataFromUrl方法。

Here are some ideas. 这里有一些想法。

  • Check your call on the client is used asynchronously. 检查您对客户端的调用是否被异步使用。 From the Metor HTTP doc: 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). 从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). 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).

  • I had CORS problem too in one of my project and I ended up by using HTTP library server side only. 我在我的一个项目中也遇到了CORS问题,最终只使用HTTP库服务器端。 You can do it by surrounding your HTTP call with a Meteor.isServer. 您可以通过用Meteor.isServer环绕HTTP调用来实现。

Somebody tried to answer But I am not getting it. 有人试图回答,但我不明白。 You try your luck. 你试试运气。 https://codexample.org/questions/9358/no-access-control-allow-origin-error-in-meteor-app.c 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"
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM