“我可以通过使用 CORS Chrome 扩展来解决此问题。但是,在这种情况下,该应用程序只能在本地主机上运行。部署后将无法运行。
我正在使用的API:
https://www.swiggy.com/dapi/restaurants/list/v5?lat=18.97210&lng=72.82460&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING
”
import RestaurantCard, { withPromotedLabel } from "./RestaurantCard";
import { useState, useEffect, useContext } from "react";
import Shimmer from "./Shimmer";
import { Link } from "react-router-dom";
import useOnlineStatus from "../utils/useOnlineStatus";
import UserContext from "../utils/UserContext";
const Body = () => {
const [listOfRestaurants, setListOfRestaurants] = useState([]);
const [filteredRestaurant, setFilteredRestaurant] = useState([]);
const [searchText, setSearchText] = useState("");
const RestaurantCardPromoted = withPromotedLabel(RestaurantCard);
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
const data = await fetch(
"https://www.swiggy.com/dapi/restaurants/list/v5?lat=18.97210&lng=72.82460&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
);
const json = await data.json();
setListOfRestaurants(json?.data?.cards[4]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setFilteredRestaurant(json?.data?.cards[4]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
};
const onlineStatus = useOnlineStatus();
if (onlineStatus === false) {
return (
<h1>
Looks like you're offline!! Please check your internet connection.
</h1>
);
}
const { loggedInUser, setUserName } = useContext(UserContext);
// Conditional Rendering
return listOfRestaurants.length === 0 ? <Shimmer /> : (
<div className="body">
<div className="filter flex">
<div className="search m-4 p-4">
<input
type="text"
data-testid="searchInput"
className="border border-solid border-black"
value={searchText}
onChange={(e) => {
setSearchText(e.target.value);
}}
/>
<button
className="px-4 py-2 bg-green-100 m-4 rounded-lg"
onClick={() => {
const filteredRestaurant = listOfRestaurants.filter(
(res) => res.info.name.toLowerCase().includes(searchText.toLowerCase())
);
setFilteredRestaurant(filteredRestaurant);
}}
>
Search
</button>
</div>
<div className="search m-4 p-4 flex items-center">
<button
className="px-4 py-2 bg-gray-100 rounded-lg"
onClick={() => {
const filteredList = listOfRestaurants.filter(
(res) => res.info.avgRating > 4
);
setFilteredRestaurant(filteredList);
}}
>
Top Rated Restaurants
</button>
</div>
<div className="search m-4 p-4 flex items-center">
<label className="p-2 pl-4">UserName:</label>
<input
className="border border-black p-2"
value={loggedInUser}
onChange={(e) => setUserName(e.target.value)}
/>
</div>
</div>
{/* Restaurant cards rendering */}
<div className="restaurant-list">
{filteredRestaurant.map((restaurant) => (
<Link to={`/restaurant/${restaurant.info.id}`} key={restaurant.info.id}>
{restaurant.info.veg ? (
<RestaurantCardPromoted resData={restaurant} />
) : (
<RestaurantCard resData={restaurant} />
)}
</Link>
))}
</div>
</div>
);
};
export default Body;
如果API不面向公众(仅适用于他们的公司)并且我检查了没有与之相关的文档,我认为您不能使用该API。但是,如果您想确定,您可以在
package.json
中使用代理,如下所示:
{
"name": "your-app-name",
"version": "1.0.0",
"proxy": "https://www.swiggy.com" // add this line
}