2016-02-29 56 views
0

我從Jquery中的表單提交後調用一個快速端點。所以當表單提交時,它會調用signUpUser(value),然後向express服務器發起一個ajax請求。快遞端點給出404

呼叫/註冊導致404,我以爲我正在設置端點正確。

它給出404的任何理由?我試過GET/POST和其他一些迭代。

enter image description here

$(document).ready(function(){ 
 
\t $('#signupForm').submit(function() { 
 
\t \t console.log("here"); 
 
\t \t console.log("yup"); 
 
\t \t var value=$("#email").val(); 
 
\t \t signUpUser(value); 
 
\t }); 
 

 

 
\t var signUpUser = function (value){ 
 
\t \t console.log("yaaa"); 
 
\t \t $.ajax({ 
 
\t \t \t type:"GET", 
 
\t \t \t url:"/signup" 
 
\t \t  }) 
 
\t \t \t .done(function(){ 
 
\t \t \t \t window.location='/confirmation.html'; 
 
\t \t \t }) 
 
\t \t \t .fail(function(){ 
 
\t \t \t \t alert('An error occurred while trying to sign up. Please try again.') 
 
\t \t \t }); 
 
\t }; 
 
});

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) { 
 
\t $.ajax({ 
 
\t \t \t type:"POST", 
 
\t \t \t url:"https://us11.api.mailchimp.com/3.0/lists/8085fb931b/members", 
 
\t \t \t user: 'anystring:XX', 
 
\t \t \t header: 'content-type: application/x-www-form-urlencoded', 
 
\t   data: { "email_address": "[email protected]", 
 
\t        "status": "subscribed" 
 
\t    } 
 
\t \t  }) 
 
\t \t \t .done(function(){ 
 
\t \t \t \t window.location='/confirmation.html'; 
 
\t \t \t \t res.send(200); 
 
\t \t \t }) 
 
\t \t \t .fail(function(){ 
 
\t \t \t \t alert('An error occurred while trying to sign up. Please try again.') 
 
\t }); 
 
});
<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>

回答

0

你想打,/註冊的終點,是一個聲明爲POST終點。您的ajax請求中的類型屬性是GET。您正在獲取404,因爲您正嘗試向不存在的GET端點發出請求。