简体   繁体   中英

Express Endpoint Giving a 404

I'm calling an express endpoint from after form submission in Jquery. So when the form submits, it calls signUpUser(value) which then initiates an ajax request to the express server.

The call to /signup is resulting in a 404, I thought I was setting up the endpoint properly.

Any reason it is giving a 404? I've tried GET/POST and a few other iterations.

在此处输入图片说明

 $(document).ready(function(){ $('#signupForm').submit(function() { console.log("here"); console.log("yup"); var value=$("#email").val(); signUpUser(value); }); var signUpUser = function (value){ console.log("yaaa"); $.ajax({ type:"GET", url:"/signup" }) .done(function(){ window.location='/confirmation.html'; }) .fail(function(){ alert('An error occurred while trying to sign up. Please try again.') }); }; }); 

 var express = require('express'); var app = express(); var cors = require('cors'); var path = require('path'); var bodyParser = require('body-parser'); var http = require('http'); var fs = require('fs'); app.set('port', (process.env.PORT || 5000)); app.use(express.static(__dirname + '/public')); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(cors()); var router = express.Router(); app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/index.html')); }); app.get('/thesis.html', function(req, res) { res.sendFile(path.join(__dirname + '/thesis.html')); }); app.get('/confirmation.html', function(req, res) { res.sendFile(path.join(__dirname + '/confirmation.html')); }); app.get('/about.html', function(req, res) { res.sendFile(path.join(__dirname + '/about.html')); }); app.post('/signup', function (req, res) { $.ajax({ type:"POST", url:"https://us11.api.mailchimp.com/3.0/lists/8085fb931b/members", user: 'anystring:XX', header: 'content-type: application/x-www-form-urlencoded', data: { "email_address": "ttttt@ssssssss.com", "status": "subscribed" } }) .done(function(){ window.location='/confirmation.html'; res.send(200); }) .fail(function(){ alert('An error occurred while trying to sign up. Please try again.') }); }); 
  <form id="signupForm" ng-controller="formController"> <fieldset> <input type="test" id="email" name="field1" id="field1" ng-model="email"> <input type="submit" value="Create Profile"> </fieldset> </form> 

The end point you're trying to hit, /signup, is a declared as a POST end point. The type attribute in your ajax request is GET. You're getting a 404 because you're trying to make a request to a GET end point that doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM