【发布时间】:2019-10-22 12:35:10
【问题描述】:
编辑:已解决,在下面发布详细信息作为答案...非常愚蠢的用户错误
我正在尝试为 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 的长错误块并出现错误
"data: { error: 'passed value not string or JSON object' } }"
完整的错误日志(pastebin,因为它很长):https://pastebin.com/aLguKQ2C
邮递员截图
【问题讨论】:
-
为什么在 URL 上使用
JSON.stringify()?你能分享一些在 Postman 中工作的截图吗? -
仅供参考,Axios 默认内容类型已经是 JSON。你可以使用
axios.post('game_event', data) -
忘了提到@Phil,没有 stringify() 也遇到了同样的问题,并决定将它添加到那里。邮递员截图:imgur.com/9gEef9n。下划线无关紧要,因为它只是为将来通过绑定到引擎的事件发送文本时选择的标识符。诉诸默认标题也有相同的结果
-
邮递员发布到
bind_game_event,但您的代码发布到game_event,所以它们肯定不一样 -
是的...一秒钟前注意到了自己。无论如何感谢您的回答,下次我会采取额外的步骤来消除这样的错别字。随意发布作为答案,以便我可以标记为已解决。
标签: javascript node.js json axios