编辑:已解决,在下面发布详细信息作为答案...非常愚蠢的用户错误
我正在尝试为 SteelSeries Gamesense 编写简单的插件,以在 GameDAC 屏幕上显示当前从 iTunes 播放的歌曲。基本上,引擎通过提供的服务器工作,监听带有 JSON 正文的发布请求。很长一段时间以来,我一直在努力使我的请求生效,但没有成功。
我在 Postman 上测试了请求,它应该按预期工作,所以问题可能出在语法上。
const axios = require('axios');
const coreProps = require(process.env.ProgramData + '/SteelSeries/SteelSeries Engine 3/coreProps.json');
const url = JSON.stringify(coreProps['address']);
axios.defaults.baseURL = 'http://' + url.replace(/"/g,'');
axios.defaults.headers['post'] = {'Content-Type': 'application/json'};
console.log(axios.defaults.headers);
function bind_itunes() {
const data = {
"game": "ITUNES",
"event": "NOWPLAYING",
"handlers": [
{
"device-type": "screened",
"zone": "one",
"mode": "screen",
"datas": [
{
"has-text": true,
"context-frame-key": "songname"
}
]
}
]
};
axios.post('game_event', JSON.stringify(data))
.then((res) => {
console.log(res)
}).catch((error) => {
console.error(error)
})
}
bind_itunes();
代码因来自 Axios 的错误长错误 block 而失败
"data: { error: 'passed value not string or JSON object' } }"
完整的错误日志(pastebin 因为它很长):https://pastebin.com/aLguKQ2C
postman 截图
最佳答案
下次提问之前,我还会确保对 API 端点进行三次检查。
如比较屏幕截图和代码时所见,我在错误的端点(game_event 而不是 bind_game_event)上进行轮询,这很明显会导致请求错误。
经过数小时的琢磨,终于解决了这个问题。
感谢大家的帮助,抱歉打扰了。
关于javascript - 使用 Axios 发布 JSON ('passed value not string or JSON object' 的问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486922/