Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

maria ramirez
maria ramirez
12,068 Points

contact form for react app -getting 404 error

Hi!, For some reason I cannot get this contact for to post to a mailer.php file I tried it with a full file path too but I keep getting POST http://localhost:3000/mailer.php 404 (Not Found) I don't understand why this is happening.. Here is my code:

import React, {Component} from 'react'
import axios from 'axios'
import $ from 'jquery'

export default class Contact extends Component{
  constructor(){
    super()
    this.state={
      name:"",
      email:"",
      message:""
    }
    this.handleSubmit=this.handleSubmit.bind(this)
    this.handleChange=this.handleChange.bind(this)

  }

  handleChange(e){
    let target=e.target
    let value=target.value
    let name=target.name
    this.setState({
      [name]: value
    })
  }

  handleSubmit(e){
    e.preventDefault
    $.ajax({
      type:'POST',
      url:'/mailer.php',
      data: {
        'name': this.state.name,
        'email': this.state.email,
        'message': this.state.message
      }
    }).done( function(response){
            console.log('success!')
      this.setState({
        name: '',
        email: '',
        message: ''
      })
}).fail(function (data){
      console.log('error')
    })
  }


  render(){
    return(
      <div className="contact-container">
            <form  onSubmit={this.handleSubmit}  >
              <h1>Stay in Touch!</h1>
              <div className="form-input">
                <label for="fname">Name</label>
                <input type="text" id="fname" name="name" value={this.state.name} onChange={this.handleChange}required/>
              </div>
              <div className="form-input" >
                <label for="email">Email</label>
                <input type="text" id="email" name="email" value={this.state.email} onChange={this.handleChange} required/>
              </div>
              <div className="form-input message">
                <label for="message">Message</label>
                <textarea id="message" name="message" value={this.state.message} onChange={this.handleChange} required></textarea>
              </div>
              <div className="form-input">
                <input type="submit" value="Send"/>
                </div>
            </form>
      </div>
    )
  }

}