Fetch: 404 when trying to replicate jQuery ajax POST request

Created on 9 Jun 2016  路  2Comments  路  Source: github/fetch

Hello! I'm trying to send some basic form data via a POST request.

The following works for me using jQuery's AJAX:

$.ajax({
    type: 'POST',
    url: 'api/form',
    data: data,
    dataType: 'json'
});

However the following does not work for me using fetch:

fetch('api/form', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: data
});

I have tried sending it both as JSON and as a serialized string, i.e. name=Brian&email=brian%40butterfield.com&message=Test+message, both give me the message contactForm.js:83 POST http://localhost:3000/api/form 404 (Not Found).

Any idea why this wouldn't be working?

I've attached screenshots of my network panel for both below:

network-stuff

Most helpful comment

Looks like I specifically needed the header 'X-Requested-With': 'XMLHttpRequest', seems to be working now!

All 2 comments

Looks like I specifically needed the header 'X-Requested-With': 'XMLHttpRequest', seems to be working now!

鏌ョ湅涓媢rl

Was this page helpful?
0 / 5 - 0 ratings

Related issues

indranildutta06 picture indranildutta06  路  3Comments

gkatsanos picture gkatsanos  路  4Comments

hannesvdvreken picture hannesvdvreken  路  4Comments

shirotech picture shirotech  路  3Comments

codeashian picture codeashian  路  3Comments