【问题标题】:FCM Sending HTTP Post request gives error code 400()FCM 发送 HTTP Post 请求给出错误代码 400()
【发布时间】:2017-11-24 06:14:30
【问题描述】:

我正在使用 HTTP POST 请求向我的网络应用程序上的作者发出通知,他们的故事已获得我们的管理员的批准。

我的payload是这样的

var payload = {
  "notification": {
    "title": "Story Approved!",
    "body": "Your story is approved"
  },
  "to" : to
}

这是我的要求

$.ajax({
  type: 'POST',
  url: 'https://fcm.googleapis.com/fcm/send',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key='+serverKey
  },
  data: payload,
  success: function(response){
    console.log(response);
  },
});

每当我尝试发送请求时,都会出现以下错误

POSThttps://fcm.googleapis.com/fcm/send400()

我对此有点陌生,根据Firebase Error Response Codes

Invalid JSON 400 检查 JSON 消息格式是否正确并包含有效字段(例如,确保传入正确的数据类型)。

但我不知道我错在哪里。任何帮助将不胜感激

【问题讨论】:

    标签: javascript firebase-cloud-messaging


    【解决方案1】:

    只需像这样将您的有效负载转换为 json:

     data: JSON.stringify(msg)
    

    你的代码会是这样的:

    $.ajax({
      type: 'POST',
      url: 'https://fcm.googleapis.com/fcm/send',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'key='+serverKey
      },
      data: JSON.stringify(msg),
      success: function(response){
        console.log(response);
      },
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2013-12-11
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多