【问题标题】:Why my POST request return the index.html react just on production?为什么我的 POST 请求返回 index.html 只对生产做出反应?
【发布时间】:2023-01-14 00:04:40
【问题描述】:

我的应用程序包含一个用 react 和简单的 php 开发的简单联系表单,用于将邮件发送给填写表单的用户。

在本地主机上它工作正常并将电子邮件发送给用户,但是当我在生产环境中运行时,请求返回反应项目的 index.html。

我的代码反应:

import React, { Fragment, useState } from 'react';
import axios from 'axios';

const Form = () => {
    const [userData, setUserData] = useState({
        name: '',
        entity: '',
        email: '',
        phone: '',
        message: ''
    })

    const handleInputChange = (event) => {
        setUserData({
            ...userData,
            [event.target.name]: event.target.value
        })
    }

    const sendData= (event) => {
        event.preventDefault();

        const url = 'https://mydomain.org.co/api_contact/?contact=1';

        axios.post(url, userData)
            .then(response => {
                let data = response.data;
                console.log("data:", data);
            })

    }

    return (
        <Fragment>
           {/* ... FORM CODE */}
        </Fragment>
    );
}

export default Form;

我在服务器中的文件夹 (public_html)

enter image description here

我的 .htaccess 文件

RewriteOptions inherit

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

【问题讨论】:

  • 您应该在您的 api_contact 项目上设置一个基本的 GET 请求,并随机调用一些东西,看看您的应用程序是否可以通过点击 URL 来访问您的 API 并查看它是否返回响应。这样你就会知道你的生产环境是否能够首先访问 API

标签: php reactjs api post request


【解决方案1】:

首先 - 验证您的生产服务器是否支持 php。 其次 - 观察您是否实现了在导航器上执行https://mydomain.org.co/api_contact/?contact=1。 三。尝试使用 postman 等工具执行 POST 调用。

三个步骤你会解决一些问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2019-05-16
    • 2016-11-02
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多