[英]I want to get request from an api url using fetch method. But I keep on getting error 400
I want to get request from an api url using fetch method.我想使用 fetch 方法从 api url 获取请求。 But I keep on getting
error 400
但我不断收到
error 400
This is my script这是我的脚本
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
I already test this api request using postman and it is successful, but why when I implemented in my website it is unsuccessful?我已经使用 postman 测试了这个 api 请求并且它是成功的,但是为什么当我在我的网站中实现它时它不成功?
You're not passing the required text
param in the fetch
call causing the API to return a 400
error.您没有在
fetch
调用中传递所需的text
参数,导致API返回400
错误。 And you can omit { method: 'get' }
since it's the default.您可以省略
{ 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. 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.