好吧,我有一个nodeJs / Express Webservice,当我通过Postman将帖子发送到webservice时,当我用JS发送帖子时,它就可以放了。我收到错误{消息:“无法读取未定义的属性'路径'”}
好,当我通过邮递员向http://localhost:5000/api/users发送帖子时 并且在正文中的表单数据字段中有此userImage,它可以正常工作。但是当我通过JS客户端发送时,出现错误
好,这是JS CLIENT
$("#registerUser").click(async e => {
e.preventDefault();
let image = $("#fileToUpload").val();
let number = $("#phone").val();
let username = $("#username").val();
let firstname = $("#firstname").val();
let lastname = $("#lastname").val();
let password = $("#password").val();
let email = $("#email").val();
let level = $("#user_level").val();
let data = JSON.stringify({
userImage: image,
username: username,
password: password,
firstname: firstname,
lastname: lastname,
email: email,
phone: number,
role: level
});
console.log(data);
registerUser(data);
});
async function registerUser(data) {
try {
console.log(data);
let response = await fetch("http://localhost:5000/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: cookie
},
body: data
});
let result = await response.json();
console.log(result);
} catch (e) {
console.log("Error: " + e.message);
}
}
答案 0 :(得分:0)
我想你必须:
app.use(bodyParser.json())
并启用cors