获取 GET 请求获取 400(错误请求)
fetch GET request getting 400 (Bad Request)
将查询字符串传递给 fetch() 中的 URL 时出现 'GET 400 bad request' 错误
import React, { useState, useEffect } from 'react';
import Card from '../UI/Card';
import './Search.css';
const Search = React.memo(props => {
const { onLoadIngredients } = props;
const [enteredFilter, setEnteredFilter] = useState('');
useEffect(() => {
const query = enteredFilter.length === 0 ? '' : `?orderBy="title"&equalTo="${enteredFilter}"`;
fetch('https://react-hooks-update-45be2-default-rtdb.firebaseio.com/ingredients.json/' + query)
.then(response => response.json())
.then(responseData => {
const loadedIngredients = [];
for (const key in responseData) {
loadedIngredients.push({
id: key,
title: responseData[key].title,
amount: responseData[key].amount
});
}
onLoadIngredients(loadedIngredients);
});
}, [enteredFilter, onLoadIngredients]);
return (
<section className="search">
<Card>
<div className="search-input">
<label>Filter by Title</label>
<input
type="text"
value={enteredFilter}
onChange={event => setEnteredFilter(event.target.value)}
/>
</div>
</Card>
</section>
);
});
export default Search;
这是错误信息:
Search.js:15 GET https://react-hooks-update-45be2-default-rtdb.firebaseio.com/ingredients.json/?orderBy=%22title%22&equalTo=%22Apple%22 400(错误请求)
这是 firebase 错误
直到 json 路线正常。但是过滤器 url 的样式是 错误的 .
请检查这个Link
这个link和你的问题一样。
将查询字符串传递给 fetch() 中的 URL 时出现 'GET 400 bad request' 错误
import React, { useState, useEffect } from 'react';
import Card from '../UI/Card';
import './Search.css';
const Search = React.memo(props => {
const { onLoadIngredients } = props;
const [enteredFilter, setEnteredFilter] = useState('');
useEffect(() => {
const query = enteredFilter.length === 0 ? '' : `?orderBy="title"&equalTo="${enteredFilter}"`;
fetch('https://react-hooks-update-45be2-default-rtdb.firebaseio.com/ingredients.json/' + query)
.then(response => response.json())
.then(responseData => {
const loadedIngredients = [];
for (const key in responseData) {
loadedIngredients.push({
id: key,
title: responseData[key].title,
amount: responseData[key].amount
});
}
onLoadIngredients(loadedIngredients);
});
}, [enteredFilter, onLoadIngredients]);
return (
<section className="search">
<Card>
<div className="search-input">
<label>Filter by Title</label>
<input
type="text"
value={enteredFilter}
onChange={event => setEnteredFilter(event.target.value)}
/>
</div>
</Card>
</section>
);
});
export default Search;
这是错误信息: Search.js:15 GET https://react-hooks-update-45be2-default-rtdb.firebaseio.com/ingredients.json/?orderBy=%22title%22&equalTo=%22Apple%22 400(错误请求)
这是 firebase 错误
直到 json 路线正常。但是过滤器 url 的样式是 错误的 .
请检查这个Link
这个link和你的问题一样。