![](/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.