【发布时间】:2017-05-16 18:23:48
【问题描述】:
我有一个表单我试图从客户端提交到 Express,但每次我收到如下错误:
无法 POST /request_method
下面是我正在尝试的代码:
index.html
<form id="wizard-content" method="post">
<label>File</label>
<input type="file" name="some" id="rsome">
<label>Value</label>
<input type="text" name="valSome" id="perfect">
</form>
<button type="submit" id="submit_form">Finish</button>
<script type="text/javascript">
jQuery('#submit_form').click(function() {
if (jQuery(this).text().toLowerCase() === "finish") {
submitForm();
}
});
var submitForm = function(){
var formData = {
'perfect' : $('#perfect').val(),
'rsome' : $('#rsome')[0].files[0]
};
if(formData){
$.ajax({
url : '/request_method',
type : 'POST',
data : formData,
contentType : false,
cache : false,
processData: false,
success : function(response){
console.log(response);
},
error : function(error){
console.log(error);
}
});
}
}
</script>
在 expressJs 中:
server.js
var express = require('express');
var bodyParser = require('body-parser')
app.use(bodyParser());
.
.
router.post('/request_method', function(req, res){
console.log(req.body);
console.log(req.ip);
});
【问题讨论】:
-
把整个网址的名字..eg.localhost:5000/xxxx
-
还有其他错误信息吗?响应是什么状态(例如检查 devtools)
-
@ovidiuDolha 我没有收到任何其他错误,这是一个错误,我收到 404 响应。
-
@sabareesh 我已经尝试使用 (window.location.origin+method_name) 仍然遇到同样的错误。