【问题标题】:Method PUT is not allowed by Access-Control-Allow-Methods Angular 6 firebaseAccess-Control-Allow-Methods Angular 6 firebase不允许方法PUT
【发布时间】:2019-09-29 20:38:44
【问题描述】:

当我尝试本地提供firebase功能时,我在更新nodejs和npm版本后遇到了一个奇怪的问题(在部署一切正常之后)。

详情: 当我尝试从 angular6 应用程序向本地提供的 fireabse 发送 put 和/或 delete 请求时,控制台错误是这样的:

访问 XMLHttpRequest 在 'http://localhost:5000/PROJECT-NAME/us-central1/helloWorld' 来自原点“http://localhost:4200”已被 CORS 策略阻止: 预检中的 Access-Control-Allow-Methods 不允许方法 PUT 回应。

但是在发布/获取请求的情况下,一切都很好

版本:

NodeJS: 10.15.3
NPM : 6.4.1
firebase : 6.9.1
OS: macOS Mojave (10.14.4)

我在服务器中使用 CORS;

    const cors = require('cors')({ origin: true });
    exports.helloWorld = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        res.send({message: "Hello from Firebase!"});    
    })

});

这是我的 package.json 文件

    {
      "name": "functions",
      "scripts": {
        "lint": "tslint --project tsconfig.json",
        "build": "tsc --skipLibCheck",
        "serve": "npm run build && firebase serve --only functions",
        "shell": "npm run build && firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log"
      },
      "main": "lib/index.js",
      "dependencies": {
        "@google-cloud/firestore": "^0.18.0",
        "@sendgrid/mail": "^6.3.1",
        "axios": "^0.18.0",
        "core-util-is": "^1.0.2",
        "cors": "^2.8.5",
        "express": "^4.16.4",
        "fetch-base64": "^2.1.2",
        "firebase-admin": "^7.3.0",
        "firebase-functions": "^2.3.1",
        "image-to-base64": "^2.0.1",
        "request": "^2.88.0",
        "request-promise": "^4.2.2",
        "string-similarity": "^3.0.0",
        "stripe": "^6.26.1",
        "twilio": "^3.29.1"
      },
      "devDependencies": {
        "firebase-functions-test": "^0.1.6",
        "tslint": "~5.8.0",
        "typescript": "^3.3.3"
      },
      "private": true,
      "engines": {
        "node": "10"
      }
    }

这是服务器结果的截图

【问题讨论】:

  • 有机会回答吗?
  • 自从升级到 firebase-tools@6.9 后,这也开始发生在我身上。尝试将 serve 命令更改为 firebase emulators:start --only functions edit 可能是值得的 - 跟踪此 github.com/firebase/firebase-tools/issues/1275 的 github 问题
  • 谢谢 Alex!,我已经降级了,现在可以正常工作了。
  • 6.11.0 版还是不行

标签: node.js angular firebase google-cloud-functions


【解决方案1】:

答案如下:

问题出现在我在问题中提到的 firebase-tools 更新之后。 6.9.0 和 6.9.1 的版本有这个问题。 只需我降级到 6.8.0,现在它对我来说工作正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 2013-12-14
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多