Linked Questions

Popular Questions

Can't POST through React to Rails API

Asked by At

I'm trying to use fetch() to POST React form data to my Rails API, but my error within the Network tab of chrome dev tools returns:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<pre>Cannot POST /undefined/cars</pre>

The error in the console states the "Unexpected token '<'" indicating that my response is being sent as HTML instead of JSON, but I'm not sure why it's not converting.

Here's my fetch request:

export const createCar = car => {
return dispatch => {
    return fetch(`${API_URL}/cars`, {
        method: "POST",
        headers: {
            'Content-type': 'application/json'
        body: JSON.stringify({ car: car })
    .then(response => response.json())
    .then(car => {
    .catch(error => console.log(error + 'createCar POST failed'))


Could someone please help me with this? Thanks.

Related Questions