简体   繁体   English

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

[英]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.

相关问题 使用 Fetch API 提交 WordPress 表单时,我不断收到 400(错误请求) - I keep getting a 400 (Bad Request) when submitting a WordPress form using the Fetch API 我想获取我的 Graph API 令牌,但出现错误,我正在使用 HttpServices 发出 POST 请求。 我收到 400 状态码。 我正在使用 NestJS - I want to get my Graph API token but I got an error, I'm making a POST request with HttpServices. I'm getting a 400 status code. I'm using NestJS 我想从网站获取 JSON,但不断收到 401 错误 - I want to fetch a JSON from a website, but keep getting the 401 error 为什么我尝试使用 fetch API 从 html 页面获取数据时出现 400 错误? - Why do I get a 400 error trying to get data from an html page using fetch API? 当我尝试使用jQuery从Twitter API获取数据时收到错误请求(400) - Getting a Bad Request (400) when I try to GET data from the Twitter API using jQuery 当获取 url 没问题时,为什么我会收到错误 400? - Why am I getting the error 400, when the fetch url is ok? 我想在 get 方法中使用 promise 返回值。 nodejs休息api - I want to use a promise return value inside the get method. nodejs rest api 使用 filter 方法检查数组每个元素的第一个字母是否大写。 但我不断得到整个数组 - Check if the first letter of each element of an array is capital using filter method. But I keep getting the whole array 为什么我在从 .net 核心控制器调用 ajax 数据表 api 时不断收到 400 错误? - Why i keep getting 400 error while calling ajax datatable api from .net core controller? 运行 axios 获取请求时收到 400 错误代码? - Getting 400 error code when I run axios get request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM