【问题标题】:Why I get a "Bad Request" Error 400 when I try to connect with Api with fetch?为什么我在尝试使用 fetch 连接 Api 时收到“错误请求”错误 400?
【发布时间】:2021-03-27 03:57:15
【问题描述】:

我遇到了 FE/BE 集成的问题。当我尝试从 PHP api 取回优惠对象时,我收到 400 错误。

我使用代理获取 API。

class OfferSite extends Component {
        state = {
                siteId: 1,
                offerId: 4,
                offerDetails: "",

        };

        componentDidMount = () => {

                const data = {
                        "siteId" : this.state.siteId,
                        "offerId" : this.state.offerId,
                };



                fetch('/v1/offers/details', {
                        method: "POST",
                        headers: {'Content-Type': 'application/json'},
                        body: JSON.stringify({"siteId" : 1, "offerId": 4}),
                })

                    .then((response) => response.json())
                    .then((data) => {
                            this.setState({offerDetails: data});
                            console.log(data);
                            })
        };

        render() {

                return (
                    <div>
                            <OfferCover />
                            <OfferMenu />
                            <OfferHighlights />
                            <OfferMovie />
                            <OfferResort />
                            <OfferAccomodation />
                            <OfferAttractions />
                            <OfferSchedule />
                            <OfferTransport />
                            <OfferCourses />
                            <OfferPrice />
                            <OfferAddons />
                            <OfferTrips />
                    </div>
                )


        }
}

export default OfferSite;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

这是我得到的错误:

[object%20Module]:1 GET http://localhost:3000/[object%20Module] 400 (Bad Request)

我已经花了大约 6 个小时试图自己找到解决方案,如果有任何帮助,我将不胜感激 :)

【问题讨论】:

    标签: php reactjs api


    【解决方案1】:

    问题:


    Error 400 表示后端服务器无法处理您的请求。也就是说,你的请求不符合后端服务器的规则。

    解决方案:


    乍一看,API调用语法似乎没有问题。首先,使用Postman 检查您的请求,以确保 API 端点正常工作。

    【讨论】:

    • 非常感谢!确实这是API的一些问题。稍作改动后一切正常!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多