如何使用 json 内容类型和语义 UI API 发出 POST 请求

How to make a POST request with json content type with Semantic UI API

我正在使用带有语义 UI 的 API feature 下拉菜单,并想对我的 API 执行 POST 请求以搜索用户

默认情况下,语义 UI 的 API 功能发送表单数据内容类型。

为了强制使用正确的内容类型,我做了类似的事情:

receiverDropdown
    .dropdown({
        apiSettings: {
            url: '/api/search/',
            method: 'POST',
            beforeXHR: (xhr) => {
                xhr.setRequestHeader('Content-Type', 'application/json');
            },
            beforeSend: (settings) => {
                settings.data = {
                    type: 'user',
                    query: 'test'
                }
                return settings
            }
        }
    })

但数据仍然以表单数据的方式发送,例如 type=user&query=test 而不是真正的 JSON 对象负载,例如 {type: 'user', query: 'test'}

如何将带有语义 UI API 的 json 对象发送到我自己的 POST API 路由?

使用JSON.stringify:

JSON.stringify({type: 'user', query: 'test'})