[英]Am I misusing my POST request? I keep getting 400/500 (bad request)
I have been working very hard on a Website for my final project in a course at my university.我一直在为我大学课程中的最后一个项目而在网站上努力工作。 I am having difficulty with understanding why my $.ajax POST request is sending NULL over to my server, when it should ( AND USE TO IN THE PAST) send over the name of my file I have uploaded.我很难理解为什么我的 $.ajax POST 请求将 NULL 发送到我的服务器,它应该(并且在过去使用)发送我上传的文件的名称。
Here's the code for my client side这是我的客户端的代码
$('#upload-file').on('change', function (e) {
let uploadname = e.target.files[0].name;
console.log("ICS NAME "+ e.target.files[0].name);
let icsFlag = false;
let formData = new FormData();
formData.append('uploadFile', e.target.files[0].name);
console.log("FORM:" + formData.uploadname);
let fileName = e.target.files[0].name;
if (checkICS(e.target.files[0].name) == true) {
$.ajax({
type: 'POST', //Request type
url: '/upload', //The server endpoint we are connecting to
data: formData,
processData: false,
contentType: false,
success: function (data) {
(the code won't go into either the success or failure section I just get the 400 bad post error) (代码不会进入成功或失败部分,我只是收到 400 bad post 错误)
here is my server-side code (This is the same for everyone in the class to my understanding)这是我的服务器端代码(这对我理解的课堂上的每个人都是一样的)
//Respond to POST requests that upload files to uploads/ directory
app.post('/upload', function(req, res) {
console.log("PLEASE"+req.files);
if(!req.files) {
return res.status(400).send('No files were uploaded.');
}
let uploadFile = req.files.uploadFile;
// Use the mv() method to place the file somewhere on your server
uploadFile.mv('uploads/' + uploadFile.name, function(err) {
if(err) {
return res.status(500).send(err);
}
res.redirect('/');
});
});
and here's a picture of my /upload endpoint in the network section of my debug panel ( I was told this may be helpful, something to do with the content-type?) debug-upload-endpoint这是我的调试面板网络部分中我的 /upload 端点的图片(有人告诉我这可能会有所帮助,与内容类型有关?) debug-upload-endpoint
You aren't appending the actual file to FormData
, only it's name.您没有将实际文件附加到FormData
,只是它的名称。 Then your server side uploader has no file object to work with然后你的服务器端上传器没有文件对象可以使用
Change改变
formData.append('uploadFile', e.target.files[0].name);
To到
formData.append('uploadFile', e.target.files[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.