2017-04-25 3 views
1

Dies ist die Funktion, die ichCloud-Funktionen für die Feuerbasis Rückkehr Antwort 500

bin Aufruf
'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); 

Aber ich bin immer diese Fehlermeldung:

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.

Antwort

1

Der richtige Code für das, was Sie versuchen zu tun wäre:

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

jetzt sollten Sie wie https://us-central1-idntt-baed5.cloudfunctions.net/date gehen.

und Sie erlauben nicht verwenden Access-Control-Allow-Origin

+0

Hey, danke für die Antwort. Wenn ich die URL direkt treffe, funktioniert es, aber wenn ich eine XMLHttpRequest von einer statischen Seite verwende, erhalte ich folgende Fehlermeldung: 'XMLHttpRequest kann https://us-central1-idntt-baed5.cloudfunctions.net/date nicht laden. Kein 'Access-Control-Allow-Origin'-Header ist auf der angeforderten Ressource vorhanden. Origin 'https://idntt-baed5.firebaseapp.com' ist daher nicht erlaubt. Die Antwort hatte den HTTP-Statuscode 500.' – Xpark

0

Der richtige Code für das, was Sie versuchen zu tun wäre:

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

es einen Fehler wie den folgenden kann,

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. 

Bitte verwenden Dieser Header in xhr Anfrage

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

Versuchen Sie in Mozilla Firefox zu starten, Google Chrome unterstützt dies nicht aufgrund von Sicherheitsproblemen