javascript - 使用 Axios 发布 JSON ('passed value not string or JSON object' 的问题)

标签 javascript node.js json axios

编辑:已解决,在下面发布详细信息作为答案...非常愚蠢的用户错误

我正在尝试为 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 截图

enter image description here

最佳答案

下次提问之前,我还会确保对 API 端点进行三次检查。

如比较屏幕截图和代码时所见,我在错误的端点(game_event 而不是 bind_game_event)上进行轮询,这很明显会导致请求错误。

经过数小时的琢磨,终于解决了这个问题。

感谢大家的帮助,抱歉打扰了。

关于javascript - 使用 Axios 发布 JSON ('passed value not string or JSON object' 的问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486922/

相关文章:

http - Node js上的http GET请求问题

javascript - 中断 while() 并优雅地完成 Node.js

java - 在java中创建新的Json对象与使用redis中的JSON对象

asp.net-mvc - MVC 是否不会对实现 IEnumerable<T> 的模型上的其他属性进行 json 序列化?

javascript - Ember : Call Action on Input Field on Focus Out with Argument

javascript - 有没有一种方法可以在不使用表单的情况下计算一个数字并在 HTML5 中显示结果?

javascript - 为每个 jquery 的输入设置一个值

javascript - Express 使用无模式动态基本 URL 来呈现不同的页面

javascript - 访问远程nodejs应用程序中的窗口对象

java - Jersey 2.17 : How to avoid MessageBodyWriter error in Jersey Server?