簡體   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