我想使用fetch方法从api url获取请求。但我一直在找error 400

这是我的脚本
fetch('https://api.funtranslations.com/translate/braille/unicode.json', {
method: 'get'})
.then(response => response.json()) // convert to json
.then(json => console.log(json)) //print data to console
.catch(err => console.log('Request Failed', err)); // Catch errors我已经使用postman测试了这个api请求,它是成功的,但是为什么当我在我的网站上实现它时,它不成功呢?

发布于 2021-04-26 15:57:46
您没有在fetch调用中传递所需的text参数,从而导致API返回400错误。你可以省略{ method: 'get' },因为它是默认的。
const baseURL = 'https://api.funtranslations.com/translate/braille/unicode.json'
const text = 'hello'
fetch(`${baseURL}?text=${text}`)
.then(response => response.json())
.then(console.log)发布于 2021-04-26 16:01:09
在Postman中,您请求url https://api.funtranslations.com/translate/braille/unicode.json?text=hello,而在js中,您请求不带任何参数的url。
https://stackoverflow.com/questions/67262766
复制相似问题