【问题标题】:Firebase functions to get a ajax post and console.log itFirebase 函数获取 ajax 帖子和 console.log 它
【发布时间】:2018-03-06 02:43:14
【问题描述】:

基本上我想做的是:使用 ajax 发布开始日期,按下按钮时从日历中结束日期,将其发送到编写在 firebase 函数云 (some official docs here) 和 console.log 上的 nodejs 后端。

这是我的 ajax:

$("#get_excel_button").click(function() {
    var startdate = moment($("#filter_date_from").val(), "D MMMM, YYYY").unix();
    var enddate = moment($("#filter_date_to").val(), "D MMMM, YYYY").unix();
    $.ajax({
    url: 'https://us-central1-ecc-local.cloudfunctions.net/getAjax',
    dataType: "json",
    type: 'POST',
    data: {startdate: startdate, enddate: enddate},
    success: function (data) {
        var ret = jQuery.parseJSON(data);
        $('#lblResponse').html(ret.msg);
        console.log(data);
    },
    error: function (xhr, status, error) {
        console.log('Error: ' + error.message);
        $('#lblResponse').html('Error connecting to the server.');
    },
});
});

这是我在 firebase 功能方面的节点:

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


exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("test 1");
});
exports.getAjax = functions.https.onRequest((request, response) => {
    cors(request, response, () => {
        response.send("test 2");
        const app = express();
        var date = '';
        var startdate = '';
        var enddate = '';
        request.on('data', function(data) 
        {
            var date = JSON.parse(data);
            console.log('Sending Formatted date:', date);
            response.status(200).send(date);
            response.status(200).end();
        });
        request.on('end', function() 
        {   
            response.setHeader("Content-Type", "json");
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.end(date);        
        });
    });  
});

问题是: 我在响应中的网络中的 getAjax 将仅输出 test 2 ,但不会列出我的日期变量值,我尝试使用 console.log,不会工作,我认为这个错误来自 Ajax:

错误:JSON 中位置 1 的意外标记 e

如果我在功能上检查我的 firebase 控制台,我在拨打电话时会看到此日志:

10:31:38.859 AM getAjax 函数执行耗时 210 毫秒,完成 状态码:200 10:31:38.650 AM getAjax Billing account not 配置。外网不可访问,配额严重 有限的。配置结算帐号以移除这些限制

我的表单数据如下所示:

startdate:1505941200
enddate:1506373200

我不知道这里出了什么问题,如何在响应中输出我的日期?

【问题讨论】:

    标签: javascript jquery node.js ajax firebase


    【解决方案1】:

    我不确定您是否在 Cloud Function 代码中正确使用了 cors 包。您可以尝试将您的 Cloud Functions 代码修改为:

    exports.getAjax = functions.https.onRequest((request, response) => {
        cors(request, response, () => {
            console.log("request.body :", JSON.stringify(request.body)); // Your data should be available here
            console.log("request.query :", JSON.stringify(request.query)); // Maybe check request.query too
    
            // Build JavaScript object out of request parameters
            var date = {
              startdate: request.body.startdate, 
              enddate: request.body.enddate
            }
    
            // Send JavaScript object as JSON (you're actually sending
            // a JavaScript object if you use JSON.parse())
            response.status(200).send(JSON.stringify(date));
        }
    })
    

    很抱歉,我没有测试此代码,但这应该能让您了解如何清理服务器端代码。以下是如何使用 Google Cloud Functions 的 cors 包的示例: https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js

    现在,在客户端,我的 Chrome 控制台中成功运行了以下代码(数据实际上记录在控制台中),所以我想我们越来越接近了 :-)

    $.ajax({
        url: 'https://us-central1-ecc-local.cloudfunctions.net/getAjax',
        dataType: "json",
        type: 'POST',
        data: {startdate: 1505941200, enddate: 1506373200},
        success: function (data) {
            console.log("Got data", data);
        },
        error: function (xhr, status, error) {
            console.log('Error: ' + JSON.stringify(error));
            console.log('status: ' + JSON.stringify(status));
        },
    });
    

    希望这会有所帮助!

    【讨论】:

    • 有趣,现在它可以工作了,但我仍然得到:VM139:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1,但我可以在控制台中看到 startdate 和 enddate,很好,谢谢你,要补充一点,在最后 2 ,,}'' 你错过了 });正确关闭该功能。
    • 谢谢,我刚刚编辑了答案并添加了一个从 Chrome 控制台运行的 Ajax 代码示例,我们快到了!
    猜你喜欢
    • 2019-04-16
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多