2017-04-25 56 views
1

雲功能這是我調用的火力點返回響應500

'use strict'; 

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 
const express = require('express'); 
const cors = require('cors')({origin: true}); 
const router = new express.Router(); 

router.use(cors); 
router.get('*', (req, res) => { 
     res.setHeader('Access-Control-Allow-Origin', '*').status(200) 
      .send('Hello, Functions'); 
    }); 

exports.date = functions.https.onRequest(router); 

但我發現了這個錯誤的函數:

XMLHttpRequest cannot load https://us-central1-idntt-baed5.cloudfunctions.net/date . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' https://idntt-baed5.firebaseapp.com ' is therefore not allowed access. The response had HTTP status code 500.

回答

1

爲你正在嘗試做正確的代碼將是:

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

// Start writing Firebase Functions 
// https://firebase.google.com/functions/write-firebase-functions 

exports.date = functions.https.onRequest((request, response) => { 
    response.send("Hello, Functions"); 
}) 

現在你應該去這樣https://us-central1-idntt-baed5.cloudfunctions.net/date

,你不允許使用訪問控制允許來源

+0

嗨,謝謝你的回覆。當我直接打網址它的工作原理,但是當我使用一個XMLHttpRequest從一個靜態頁面我得到這個錯誤: '的XMLHttpRequest無法加載https://us-central1-idntt-baed5.cloudfunctions.net/date。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此,原產地「https://idntt-baed5.firebaseapp.com」是不允許訪問。響應有HTTP狀態代碼500.' – Xpark

0

爲你正在嘗試做正確的代碼是:

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

exports.date = functions.https.onRequest((request, response) => { 
    response.send("Hello, Functions"); 
}) 

它可能會產生這樣的錯誤,

XMLHttpRequest cannot load https://us-central1-idntt-baed5.cloudfunctions.net/date. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://idntt-baed5.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 500. 

請使用此標頭xhr要求

req.setRequestHeader('Access-Control-Allow-Origin', '*'); 

嘗試在Mozilla Firefox中運行,由於安全問題,Google Chrome不支持此功能

相關問題