[英]send post request to firebase cloud messaging by ajax?
I'm trying to send a message to my device via firebase. 我正在尝试通过firebase向我的设备发送消息。 But I got error.
但我得到了错误。 I tested it successfully on advance REST client.
我在预先的REST客户端上成功测试了它。 This is message from rest client
这是来自其他客户端的消息
Content-Type: application/json
Authorization: key=MY-KEY
Content-Length: 106
POST /fcm/send HTTP/1.1
HOST: fcm.googleapis.com
content-type: application/json
authorization: key=MY-KEY
content-length: 106
{
"to":"/topics/Self_Taught"
"notification":
{
"body":"Hello"
}
}
Based on that, I made my javascript code. 基于此,我制作了我的javascript代码。 Don't worry about gritter, it's other library, and it works normally.
不要担心gritter,它是其他库,它可以正常工作。
$.ajax({
url: "https://fcm.googleapis.com/fcm/send",
type: "POST",
contentType: "application/json",
authorization: "key=MY-KEY",
data: {
"to": "/topics/Self_Taught",
"notification": {
"body": message
}
},
success: function (result) {
$.gritter.add({
title: "",
text: result.message_id,
class_name: 'gritter-success'
});
},
error: function (result) {
$.gritter.add({
title: "",
text: result.error,
class_name: 'gritter-error'
});
}
});
And this is what I get back from result.error 这就是我从result.error中得到的回报
function () {
if (l) {
var t = l.length;
(function i(t) {
x.each(t, function (t, n) {
var r = x.type(n);
"function" === r ? e.unique && p.has(n) || l.push(n) : n && n.length && "string" !== r && i(n)
})
})
(arguments), n ? o = l.length : r && (s = t, c(r))
}
return this
}
I followed this link by change "notification" to "data", and "body" to "message". 我通过将“通知”更改为“数据”,将“正文”更改为“消息”来跟踪此链接。 But I got the same error.
但我得到了同样的错误。 https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#http_post_request
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#http_post_request
Where is my mistake? 我的错误在哪里? :( Thank you!
:( 谢谢!
Authorization needs to be part of 'headers' & notification data needs to be passed as a string. 授权需要成为“标题”的一部分,通知数据需要作为字符串传递。 Try Below: It works :)
试试下面:它有效:)
$.ajax({
type : 'POST',
url : "https://fcm.googleapis.com/fcm/send",
headers : {
Authorization : 'key=' + '<key>'
},
contentType : 'application/json',
dataType: 'json',
data: JSON.stringify({"to": "<instance ID>", "notification": {"title":"Test","body":"Test"}}),
success : function(response) {
console.log(response);
},
error : function(xhr, status, error) {
console.log(xhr.error);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.