Home > Software engineering >  Why my POST request return the index.html react just on production?
Why my POST request return the index.html react just on production?

Time:01-14

My application consists of a simple contact form developed with react and simple php to send the mail to the user who filled the form.

On localhost it works fine and sends the email to the user, but when I run in production the request returns the index.html of the react project.

My Code in react:

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;

My folders in the server (public_html)

enter image description here

My .htaccess file

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]

CodePudding user response:

First - Verify if your production server support php. Second - Observe if you achieve to execute https://mydomain.org.co/api_contact/?contact=1 on the navegator. Three. Try to execute a POST call using tools like the postman.

Some of the three steps you will solve the problem!

CodePudding user response:

SOLVED!!!

Based on the .htaccess file, you are using a rewrite rule to redirect all requests (both GET and POST) that do not match an existing file on the server to "index.html". This means that any requests you make to the server (including POST requests) that do not match an existing file on the server will be redirected to "index.html" and will not be processed as expected.

To fix this, POST requests should be excluded from this rewrite rule. A possible solution would be to add an additional condition to the rewrite rule to exclude POST requests from the redirect.

I completely modified (deleted) my .htaccess file and added these lines:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule ^ index.html [QSA,L]
  • Related