【问题标题】:Getting below errors while calling google firebase api. How to solve this调用 google firebase api 时出现以下错误。如何解决这个问题
【发布时间】:2018-11-23 11:53:45
【问题描述】:

选项 https://fcm.googleapis.com/fcm/send 404 ()

无法加载 https://fcm.googleapis.com/fcm/send:预检响应没有 HTTP ok 状态。

{readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ,
 setRequestHeader: ƒ, overrideMimeType: ƒ, …}

XHR 加载失败:OPTIONS "https://fcm.googleapis.com/fcm/send"。

这是我的代码:

$.ajax({
          type: 'POST',
          url: 'https://fcm.googleapis.com/fcm/send',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': 'key=*********'
          },
          data: {
                 "to" : '*****************************',
                 "data" : {
                     "phone" : "99*****",
                     "id" : "5****"
                        }

                 },
          success: function(response){
            console.log("success");
          },
            error : function(response) {
                console.log(response);                   
            }
        });

【问题讨论】:

    标签: javascript json ajax firebase firebase-cloud-messaging


    【解决方案1】:

    您正在执行跨域请求。为了让浏览器知道这是允许的,它会首先执行OPTIONS调用来询问服务器这个method是否允许这个endpoint。在您的情况下,此请求失败,因此无法执行实际的 POST

    底线我不认为您尝试执行的请求是从浏览器中调用的,因为您正在显示凭据,并且应该只从后端执行。

    【讨论】:

    • 很好,彼得!最后一段非常重要。该代码向网页的所有用户公开 FCM 服务器密钥。顾名思义,FCM 服务器密钥只能在服务器(或其他受信任的环境)上使用,因为拥有该密钥的任何人都可以代表您向您的用户发送通知。
    • 谢谢彼得和弗兰克
    猜你喜欢
    • 2019-08-02
    • 2023-01-07
    • 2023-02-05
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多