JavaScript语言
提示
本篇适用于浏览器环境的 JavaScript(网页、浏览器控制台、前端项目)。Node.js 服务端写法请看 使用 Node.js 代码。
# 一、一行代码发送(jQuery)
如果页面已经引入了 jQuery,一行 GET 即可发送(也是弹幕的发送方式):
<!-- 一行代码进行消息推送 -->
<script>
$.getJSON("https://www.phprm.com/services/push/trigger/你的通道码?head=这是标题&body=这是内容");
</script>
1
2
3
4
2
3
4
带中文参数时建议用 encodeURIComponent 编码:
var channelCode = "你的通道码";
var apiUrl = "https://www.phprm.com/services/push/trigger/" + channelCode
+ "?head=" + encodeURIComponent("这是标题")
+ "&body=" + encodeURIComponent("这是长内容");
$.getJSON(apiUrl, function (resp) {
console.log("推送结果:", resp);
});
1
2
3
4
5
6
7
2
3
4
5
6
7
# 二、POST 发送(原生 fetch,无需任何库)
现代浏览器都内置了 fetch,推荐用 POST + JSON 方式发送较长内容:
async function sendPush(head, body, jumpUrl) {
var channelCode = "你的通道码";
var apiUrl = "https://www.phprm.com/services/push/send/" + channelCode;
var payload = { head: head, body: body || "", url: jumpUrl || "" };
var resp = await fetch(apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json;charset=UTF-8" },
body: JSON.stringify(payload)
});
var data = await resp.json();
console.log("HTTP", resp.status, data);
return data; // {"code":0,"message":"请求成功","data":{"messageIdList":[...]}}
}
// 调用
sendPush("构建完成", "**分支**: main\n**耗时**: 38s", "https://example.com/ci/123");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 三、POST 发送(XMLHttpRequest,兼容老旧浏览器)
function sendPush(head, body) {
var channelCode = "你的通道码";
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.phprm.com/services/push/send/" + channelCode, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log("HTTP", xhr.status, xhr.responseText);
}
};
xhr.send(JSON.stringify({ head: head, body: body || "", url: "" }));
}
sendPush("测试标题", "测试内容");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 四、直接用于弹幕
在已集成 danmu.js 的页面上,调用 trigger 接口发送的内容会实时以弹幕形式飘过所有在线页面:
fetch("https://www.phprm.com/services/push/trigger/你的通道码?head=" + encodeURIComponent("新公告:今晚8点维护"));
1
注意
- 浏览器端代码会暴露通道码,仅适合在你自己可控的网页内部(如管理后台、内网页面)使用;公开页面建议由后端代发,或使用弹幕接入文档中的审核机制。
- 通道码不要硬编码提交到公开的前端 git 仓库。
Last Updated: 2026/09/11, 21:21:27