通过Javascript发布方法不起作用但在POSTMAN中工作

时间:2017-10-10 12:37:23

标签: javascript ajax firebase post postman

我正在尝试通过POST请求向Firebase Cloud Messaging发送消息给Android设备。但是当我从POSTMAN执行发布请求时,服务器以成功响应200响应。

但是当我通过AJAX请求在JAVASCRIPT中做同样的事情时,我正在获取错误400。

如何解决这个问题?

这是我通过POSTMAN发送的数据: -

url: - “https://fcm.googleapis.com/fcm/send

Content-Type:“application / json”

授权:“key = mykey”

{
    "to" : "/topics/global",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark"
    }
}

JAVASCRIPT AJAX请求: -

如果我通过AJAX请求从javascript发送相同的数据,我会得到响应400.

mydata = {
    "to" : "/topics/global",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark"
    }
}


headers= {
    "Content-Type": "application/json",
    "Authorization": "key=AIzaSy....KC1Q",
}


$.post({
    url: 'https://fcm.googleapis.com/fcm/send',
    data: mydata,
    success: function (e) {
        console.log(e+ "SUCESS") ;
    },
    headers: headers
});

1 个答案:

答案 0 :(得分:1)

请尝试将ajax方法更改为:

$.ajax({
   url: "https://fcm.googleapis.com/fcm/send",
   data: mydata,
   success:function(e){console.log(e)},
   headers: {
       "Content-Type":"application/json", 
       "Authorization":"key=mykey"
   }
})