Express Endpoint给出404

时间:2016-02-29 23:40:46

标签: javascript jquery ajax express

我在Jquery中提交表单之后调用了一个表达端点。因此,当表单提交时,它会调用signUpUser(value),然后向快速服务器发起ajax请求。

对/ signup的调用产生了404,我以为我正在正确设置端点。

有什么理由给404?我已经尝试过GET / POST和其他一些迭代。

enter image description here



$(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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您尝试点击/ /注册的终点是声明为POST终点。您的ajax请求中的type属性是GET。您正在获取404,因为您正在尝试向不存在的GET终点发出请求。