![](/img/trans.png)
[英]I keep getting a 400 (Bad Request) when submitting a WordPress form using the Fetch API
[英]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.