繁体   English   中英

我想使用 fetch 方法从 api url 获取请求。 但我不断收到错误 400

[英]I want to get request from an api url using fetch method. But I keep on getting error 400

我想使用 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 请求并且它是成功的,但是为什么当我在我的网站中实现它时它不成功?

这是我从邮递员那里得到的结果

您没有在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)

In Postman you request the url https://api.funtranslations.com/translate/braille/unicode.json?text=hello and in js you request an url without any parameters.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM