wordsAPI 获取 returns 200 状态,无数据 javascript

wordsAPI fetch returns 200 status with no data javascript

我刚刚替换为 wordsAPI https://www.wordsapi.com/

并粘贴代码以直接从他们的文档中获取数据:

const getDefinition = async (text) => {
    try {
        const res = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${text}/definitions`, {
            "method": "GET",
            "headers": {
                "x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
                "x-rapidapi-key": "MYAPIKEY"
            }
        })
        console.log(res)
    } catch (err) {
        console.log(err)
    }
}

响应对象如下所示:

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://wordsapiv1.p.rapidapi.com/words/something/synonyms"
[[Prototype]]: Response

我返回了 200,但响应没有数据?

您应该使用 res.json() 来获取数据:

const getDefinition = async (text) => {
try {
    const res = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${text}/definitions`, {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
            "x-rapidapi-key": "MYAPIKEY"
        }
    })
    const mainData = await res.json();
    console.log(mainData)
} catch (err) {
    console.log(err)
    }
}