稳定、快速、免费的 API 接口服务
随机一句世俗名句、名人名言
请求参数说明:
名称 | 必填 | 类型 | 说明 |
---|---|---|---|
code | 否 | string | JSON输出:json,JS输出:js,XML输出:xml |
返回参数说明:
名称 | 类型 | 说明 |
---|---|---|
code | string | 200:正常状态 |
返回示例:
纯文本: 没有王子的命,就要有争帝王的心。 JSON: {"code":200,"msg":"我在人生道路上迷失了方向。"} JS: function xjhitokoto(){document.write("成不了药就成为毒,不然你只是普通的水而已。");}
错误码格式说明:
名称 | 类型 | 说明 |
---|
代码示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>一言API 示例 - Star API</title>
</head>
<body>
<h1>随机句子 (JSON)</h1>
<p id="hitokoto-json"></p>
<script>
// API URL
const apiUrl = 'https://api.xhus.cn/api/sentence?code=json';
fetch(apiUrl)
.then(response => response.json()) // 将响应解析为JSON
.then(data => {
// 将获取到的句子设置到p标签中
document.getElementById('hitokoto-json').innerText = data.msg;
})
.catch(error => {
console.error('获取句子时出错:', error);
document.getElementById('hitokoto-json').innerText = '加载失败,请重试。';
});
</script>
</body>
</html>