Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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>
    )
  }

}